我需要显示一个特定目录的QTreeView,并且我想让用户有可能用RegExp过滤这些文件。QTreeView,QFileSystemModel,setRootPath和QSortFilterProxyModel用RegExp进行过滤
据我所知Qt文档我可以在标题这样提到的类实现这一点:
// Create the Models
QFileSystemModel *fileSystemModel = new QFileSystemModel(this);
QSortFilterProxyModel *proxyModel = new QSortFilterProxyModel(this);
// Set the Root Path
QModelIndex rootModelIndex = fileSystemModel->setRootPath("E:\\example");
// Assign the Model to the Proxy and the Proxy to the View
proxyModel->setSourceModel(fileSystemModel);
ui->fileSystemView->setModel(proxyModel);
// Fix the TreeView on the Root Path of the Model
ui->fileSystemView->setRootIndex(proxyModel->mapFromSource(rootModelIndex));
// Set the RegExp when the user enters it
connect(ui->nameFilterLineEdit, SIGNAL(textChanged(QString)),
proxyModel, SLOT(setFilterRegExp(QString)));
当开始该程序的树视图被正确地固定在指定的目录。但只要用户更改RegExp,它就像TreeView忘记RootIndex一样。删除RegExp LineEdit中的所有文本(或输入RegExp,如“。”)后,它再次显示所有目录(在Windows上,这意味着所有驱动器等)
我在做什么错? :/
你有没有可能分享你所做的修改?我现在遇到了这个确切的问题,但我不知道如何解决它。 – 2011-04-19 22:13:56
很遗憾,我无法再访问此项目。这是邮件列表线程:http://www.mentby.com/Group/qt-interest/qtreeview-qfilesystemmodel-setrootpath-and-qsortfilterproxymodel-with-regexp-for-filtering.html – Strayer 2011-05-23 19:44:09