【发布时间】:2011-12-10 23:30:01
【问题描述】:
我需要显示特定目录的 QTreeView,并且我想让用户能够使用 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)));
当启动程序时,TreeView 被正确地固定到指定的目录。但是一旦用户更改了 RegExp,TreeView 似乎就忘记了它的 RootIndex。删除 RegExp LineEdit 中的所有文本(或输入 RegExp,如“.”)后,它会再次显示所有目录(在 Windows 上,这意味着所有驱动器等)
我做错了什么? :/
【问题讨论】: