2015-11-27 113 views
0

我想過濾文件系統模型,只顯示擴展名爲.ncr(NCReport模板)的文件。該視圖改爲顯示所有文件。任何想法如何使過濾工作?謝謝。基於文件擴展名的過濾不工作在QFileSystemModel?

(我知道有很多其他笨拙這裏,建議表示歡迎。)

 fsmodel = new QFileSystemModel(this); 
     connect(fsmodel,SIGNAL(rootPathChanged(QString)),this,SLOT(fileSystemModelRootSetSuccessfully())); 

     // this call is required on windows to show anything in file view 
     QModelIndex rootIndex=fsmodel->setRootPath(reportdirstring); 
     // root index could be removed 
     Q_UNUSED(rootIndex); 

     fsmodel->setReadOnly(true); 

     ui->reportTemplateDirView->setModel(fsmodel); 
     ui->reportTemplateDirView->setRootIndex(fsmodel->index(reportdirstring)); 
     ui->reportTemplateDirView->expandAll(); 
     ui->reportTemplateDirView->header()->hide(); 

     // selecting the first file entry with selectFileInDirView(); requires the qtreeview to be sorted 
     // sort in desc order since that is the only way to get the first item selected? 
     ui->reportTemplateDirView->sortByColumn(0,Qt::AscendingOrder); 
     fsmodel->sort(0,Qt::AscendingOrder); 
     QStringList filters; 
     filters << "*.ncr"; 
     fsmodel->setNameFilters(filters); 
     fsmodel->setNameFilterDisables(false); 

     // hide report template directory view extra columns, 
     //type? 
     ui->reportTemplateDirView->setColumnHidden(1,true); 
     //size? 
     ui->reportTemplateDirView->setColumnHidden(2,true); 
     //date 
     ui->reportTemplateDirView->setColumnHidden(3,true); 

     #if QT_VERSION >= 0x040700 
     // as soon as QFileSystemModel has parsed the entire directory tree, tell the QTreeView to expand its hierarchy 
     // note that if there are a lot of files, this could be too inefficient. 
     // if problems arise, consider commenting this out or using a QDirModel, which could be equally inefficient though. 
     connect(fsmodel,SIGNAL(directoryLoaded(QString)),ui->reportTemplateDirView,SLOT(expandAll())); 
     connect(fsmodel,SIGNAL(directoryLoaded(QString)),this,SLOT(selectFileInDirView())); 
     #endif 

     // show a fake folder name + icon at the top of the folder tree of report template directory 
     QFileIconProvider iconProvider; 
     QIcon folderIcon=iconProvider.icon(QFileIconProvider::Folder); 
     ui->reportTemplatesLabel->setPixmap(QPixmap(folderIcon.pixmap(QSize(16,16)))); 
+1

我剛剛重新創建了您的示例,它像一個魅力一樣工作!你使用的是什麼版本的Qt? –

+0

關於MSVC 2013 Qt 5.5。我知道沒有其他異常。 – savolai

+0

可能你在像「selectFileInDirView」這樣的插槽中有某個問題?因爲否則它會按預期過濾文件夾中的文件 –

回答

0

正如亞歷山大指出,這實際工作。問題是我在其他地方設置了過濾器。 ^。^

相關問題