Question

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.

Was it helpful?

Solution 2

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.

OTHER TIPS

I am doing this for creating my Qfilesystemmodel :

QFileSystemModel *fileSystemModel = new QFileSystemModel(this);
fileSystemModel->setRootPath(path);
QStringList filters;
filters <<"*.jpeg" <<"*.pict" <<"*.PICT";
fileSystemModel->setNameFilters(filters);
fileSystemModel->setNameFilterDisables(0);
ui->treeView_2->setModel(fileSystemModel);
ui->treeView_2->setRootIndex(fileSystemModel->index(path));
ui->treeView_2->setColumnWidth(0,300);                          
connect(fileSystemModel,SIGNAL(directoryLoaded(QString)),this,SLOT(onDirLoaded(QString)));


void MainWindow::onDirLoaded(QString path)
{

QFileSystemModel *fileSystemModel=(QFileSystemModel*)ui->treeView_2->model();
QModelIndex index = fileSystemModel->index(path);
QStringList filters;

filters <<"*.jpeg" <<"*.pict" <<"*.PICT";
fileSystemModel->setNameFilters(filters);
fileSystemModel->setNameFilterDisables(0);
int rowCount = fileSystemModel->rowCount(index);
for(int i=0;i<rowCount;i++)
{
    QModelIndex mi = fileSystemModel->index(i,0,index);
    QFileInfo fileInfo = fileSystemModel->fileInfo(mi);
    if (!templist.contains(fileInfo.absoluteFilePath()))
    {
        templist << fileInfo.absoluteFilePath();
        tempNamelist << fileInfo.fileName();
    }
    if(!bloaded)
    {
        if(fileSystemModel->hasChildren(mi))
        {
            QModelIndex index1 = fileSystemModel->index(i,0,index);
            int rowCount1 = fileSystemModel->rowCount(index1);
            if(rowCount1)
            {
                onDirLoaded(fileInfo.absoluteFilePath());
            }
        }
    }
 }


}

using this i am able to create Qfilsystemmodel but it also shows those subfolders which don't have filtered files. I don't want to show those subfolders. your help will really appreciate.

Thanks,

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top