Search code examples
c++qtqtreeviewqfilesystemmodelqsortfilterproxymodel

Hide those folders don't have filtered Files


I am working on QTreeviews, One has all the drives in it. and the other one displays all the files and folders corresponding to those drives. I have applied filters for some particular files. So it shows all those folders which have those particular filters formats as well as those folders which don't have filtered files as a empty folders. Now my problem is that i don't want to show those folders which don't have my filtered files. I am using this:

QFileSystemModel *fileSystemModel = new QFileSystemModel(this);
fileSystemModel->setRootPath(path);
QStringList filters;
filters <<"*.jpeg" <<"*.pict" <<"*.PICT" <<"*.xpm" <<"*.xbm" <<"*.ppm" <<"*.pbm" <<"*.pgm" <<"*.XBM" <<"*.XPM" <<"*.PPM" <<"*.PBM" <<"*.PGM" <<"*.png" <<"*.psd" <<"*.bmp"<<"*.jp2" <<"*.tiff" <<"*.gif" <<"*.tga" <<"*.jpg" <<"*.JPG" <<"*.JPEG" <<"*.BMP" <<"*.JP2"<<"*.PSD" <<"*.PNG" <<"*.TIFF" <<"*.GIF" <<"*.TGA"  ;
fileSystemModel->setNameFilters(filters);
fileSystemModel->setNameFilterDisables(0);
fileSystemModel->setSupportedDragActions(Qt::CopyAction);
fileSystemModel->fetchMore(fileSystemModel->index(path));
ui->treeView_2->setModel(fileSystemModel);
ui->treeView_2->setRootIndex(fileSystemModel->index(path));
ui->treeView_2->setColumnWidth(0,300);

Can anyone please help me out of it. Thanks.


Solution

  • Have a look at the documentation of QSortFilterProxyModel

    You can check if a folder has any contents and then decide whether it should be filtered out of the results or not.