How to show only folders with specific files using QTreeView and QFileSystemModel?

I have a problem filtering a specific folder using QTreeView and QFileSystemModel.

I set the root of the QFileSystemModel to a specific folder - this works. But I want to show only folders containing .jpg

files. Is it possible?

I am filtering files .jpg

and it works, but my QTreeView shows all folders, even those that have no files .jpg

. Therefore, if the user tries to open any folder without files .jpg

, nothing happens.

How do I hide these folders?

Note: below is some of the code.

QStringList filterTypeFile;
filterTypeFile.append("*.jpg");
this->m_pModelTreeViewImage->setNameFilters(filterTypeFile);
this->m_pModelTreeViewImage->setNameFilterDisables(false);

this->ui->treeViewImages->setModel(this->m_pModelTreeViewImage);

      

+3


source to share


2 answers


Bad news, I'm afraid: you have to implement this yourself if you want this behavior. Filesystem readers are simply not configured for this.

There is also a reason. Hiding files is generally supported because it is quick and easy - either the filename matches your regular expression or it doesn't. Hiding directories (or making them naked - with the same difference) is much more difficult. You can't just look at the directory name; you need to scan the file system looking for open files. And hard disk access is slow. Imagine that your user is looking at a directory very close to the root of your filesystem - you have to crawl through everything. And then if the filter changes, you have to do it all again.

Also consider: The installed template for this is only hide files and never hide directories. This is likely due to the technical difficulties described above, but this has become standard user interface behavior. So if a user comes across a filesystem viewer that doesn't display all the folders they expect (or if they can't open certain folders seemingly randomly), they're going to break something.



That said, if you have every reason to do so, you can probably override QFileSystemModel to do what you want (or, otherwise, you could subclass AbstractModel to behave as you described ). Your users will acclimate, especially if there is a good reason for the new behavior.

Good luck!

+3


source


You have to get from QSortFilterProxyModel

and override the virtual bool filterAcceptsRow ( int source_row, const QModelIndex & source_parent ) const

function. Something like that

bool JPGFilter::filterAcceptsRow(int source_row, const QModelIndex &source_parent) const
{
   QFileSystemModel *fs = static_cast<QFileSystemModel*>(sourceModel());
   QModelIndex i = fs->index(source_row, 0, source_parent);
   bool accept=false;
   if( fs->hasChildren(i) ){
     for( int j=0; j<fs->rowCount(i); j++  )
       if( fs->fileInfo(fs->index(j,0,i)).suffix()=="jpg" ){
         accept=true;
         break;
       }
   }
   return accept;
}

      



I haven't tried this myself. It's slow, but should work.

-1


source







All Articles