相关疑难解决方法(0)

带有RegExp的QTreeView,QFileSystemModel,setRootPath和QSortFilterProxyModel用于过滤

我需要显示特定目录的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)));
Run Code Online (Sandbox Code Playgroud)

启动程序时,TreeView正确地固定到指定的目录.但是一旦用户更改了RegExp,看起来TreeView就会忘记它的RootIndex.删除RegExp LineEdit中的所有文本(或输入类似"."的RegExp)后,它再次显示所有目录(在Windows上,这意味着所有驱动器等)

我究竟做错了什么?:/

c++ qt

5
推荐指数
1
解决办法
4842
查看次数

标签 统计

c++ ×1

qt ×1