我正在尝试为我的查看器创建简单的文件过滤器。
想法是仅显示过滤器允许的文件并隐藏其他文件(而不是禁用它们)。
找到了一些有用的答案 herehere ,并使用这些例子我写了这个简单的代码:

QDir dir("c:/Projects/Qt/Data/spiro/");

QFileSystemModel* model = new QFileSystemModel;
model->setRootPath(dir.path());
model->setReadOnly(true);
model->setFilter(QDir::Dirs | QDir::Files | QDir::NoDotAndDotDot);

model->setNameFilters(QStringList() << "*.dbx");
// without this line, all files are displayed, filtered out are disabled
model->setNameFilterDisables(false);

m_treeView->setModel(model);

但是,它没有按预期工作。
将 line 与 setNameFilterDisables(false) 一起使用时,我根本没有得到任何文件。
这对我来说不是预期的行为,因为我希望没有通过过滤器的文件,
根本不会显示。
这在文档中描述:



那么,如何正确过滤文件并仅显示过滤的文件?

最佳答案

你忘了树设置根索引:

m_treeView->setRootIndex(model->index(dir.path()));

如果要保持目录以及过滤后的文件可见,请使用 QDir::AllDirs 标志而不是 QDir::Dirs

关于c++ - 使用 QFileSystemModel 隐藏过滤文件,setNameFilterDisables(false) 隐藏所有内容,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/29110706/

10-11 22:23
查看更多