我在使用QTreeView和QFileSystemModel过滤特定文件夹时遇到问题。
我将根QFileSystemModel设置为一个特定的文件夹--这是可行的。但我只想显示包含.jpg
文件的文件夹。这个是可能的吗?
我过滤了.jpg
文件,但我的QTreeView显示了所有文件夹,甚至那些没有.jpg
文件的文件夹也是如此。因此,如果用户试图打开某个没有.jpg
文件的文件夹,什么也不会发生。
如何隐藏这些文件夹?
注意:下面是代码的一部分。
QStringList filterTypeFile;
filterTypeFile.append("*.jpg");
this->m_pModelTreeViewImage->setNameFilters(filterTypeFile);
this->m_pModelTreeViewImage->setNameFilterDisables(false);
this->ui->treeViewImages->setModel(this->m_pModelTreeViewImage);
发布于 2012-10-07 22:32:02
您应该从QSortFilterProxyModel
派生并重新实现virtual bool filterAcceptsRow ( int source_row, const QModelIndex & source_parent ) const
函数。像这样的东西
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;
}
我自己还没试过呢。它速度很慢,但应该可以工作。
https://stackoverflow.com/questions/9589530
复制相似问题