2013-10-03 105 views
1

我的工作QTreeviews,一个在它所有的驱动器。而另一个显示所有对应于那些驱动器上的文件和文件夹。我为某些特定文件应用了过滤器。因此,它显示了有这些特定的过滤器格式,以及不具有过滤的文件为空文件夹的文件夹,所有这些文件夹。现在我的问题是,我不希望显示其没有我过滤的文件这些文件夹。我用这:隐藏这些文件夹不具有过滤的文件

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); 

任何人都可以请帮我吧。 谢谢。

回答

-1

看一看在documentation of QSortFilterProxyModel

您可以检查一个文件夹中有任何的内容,然后再决定是否应该被过滤出来的结果还是不行。

+0

感谢您的回复。我已经检查过。你能告诉我函数名吗? – Ashish

+0

您可以告诉QSortFilterProxyModel的函数名称或任何其他隐藏Qfilesystemmodel中已过滤子文件夹的方法。 – Ashish

+0

如果您阅读我提供的文档链接,则有一整段介绍如何过滤结果。 – RobbieE

0

我这样做对我的创造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()); 
      } 
     } 
    } 
} 


} 

我能够创造Qfilsystemmodel但它也显示了没有过滤的文件的子文件夹。我不想显示这些子文件夹。你的帮助将非常感激。

谢谢,