I'm using a QFileSystemModel with a QListview to display all files from a directory. I'd like to filter that model to display some categories of files like :
My current code is :
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
Filemodel = new QFileSystemModel(this) ;
Filemodel->setFilter( QDir::NoDotAndDotDot | QDir::Files ) ;
proxy_model = new QSortFilterProxyModel();
proxy_model ->setDynamicSortFilter(true);
proxy_model ->setSourceModel( Filemodel );
proxy_model ->setFilterKeyColumn(0);
ui->Filtered_tbView->setModel( proxy_model ) ;
}
(...)
/* combobox event to select file type to filter */
void MainWindow::on_FSFilter_Combo_currentIndexChanged(int index)
{
proxy_model->setFilterWildcard("*.txt"); // just a simple example here
ui->Filtered_tbView->setModel( proxy_model ) ;
}
That code doesn't display anything while all type of files are present in the directory.
Besides, things I've tried that wasn't fine for me (pointers may be useful for further readers) :
The easiest way is to use QFileSystemModel::setNameFilters
.
With the property QFileSystemModel::nameFilterDisables
you can choose between filtered out files being disabled or hidden.
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
Filemodel = new QFileSystemModel(this) ;
Filemodel->setFilter( QDir::NoDotAndDotDot | QDir::Files ) ;
QStringList filters;
filters << "*.txt";
Filemodel.setNameFilters(filters);
Filemodel.setNameFilterDisables(false);
ui->Filtered_tbView->setModel( Filemodel ) ;
}