我正在尝试过滤文件系统模型,以仅显示扩展名为.ncr(NCReport模板)的文件。该视图显示所有文件。任何想法如何使过滤工作?谢谢。
(我意识到这里有很多其他的笨拙,欢迎提出建议。)
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))));
答案 0 :(得分:0)
亚历山大指出,这确实有效。问题是我在其他地方将过滤器设置为空。 ^。^