1

我正在使用以下过滤器设置我的 QFileSystemModel:

QDir::Filters( Dirs|AllDirs|Files|Drives|NoDotAndDotDot|AllEntries )  

在我的代理模型中,我使用正则表达式按名称过滤文件:

proxy_model_->setFilterRegExp(".*\\.(cpp$|cxx$|c$|hpp$|h$)");

....然后我model_是 QFileSystemModel,我有一行:

model_->setNameFilters(QStringList(proxy->filterRegExp().pattern()));

...但是显示的文件是灰色的。为什么,以及如何使它们“正常”。

4

2 回答 2

1

实际上,不同 Qt 类之间的格式是不一致的。如果他们采用单个 QString,那么就像@HostileFork 所说的那样。然而,在这种情况下,函数setNameFilters()需要一个 QStringList,这意味着你想要:

fileModel->setNameFilters({"*.cpp", "*.cxx", "*.c", "*.hpp", "*.h"});

由于您的输入格式错误(正则表达式,而不是 Window 的通配符),所有内容都被标记为“过滤掉”,因为没有任何内容与奇怪的语法匹配。

怎么变灰了?因为默认情况下 QFileSystemModel 禁用/灰显文件被过滤(bwah?),而不是隐藏它们。这可以通过调用来改变:

fileModel->setNameFilterDisables(false);

QFileSystemModel 的 'nameFilterDisables' 属性

于 2015-02-02T23:54:39.537 回答
0

The "name filters" that are used by the QFileSystemModel aren't very well documented. But I'm going to assume they're probably the same format as the ones used by the QFileDialog in its setNameFilter(s):

http://doc.qt.nokia.com/stable/qfiledialog.html#setNameFilter

If so, those aren't regular expressions. They're an odd format of text, followed by parentheses containing command-line-terminal-style wildcards.

So I'm guessing this would work:

model_->setNameFilters(
    QStringList("Supported files (*.cpp *.cxx *.c *.hpp *.h)"));

In general, unless documentation or the name of the function indicates otherwise, I'd be careful to assume that places that take filters as a QString would know what to make of a regular expression!

于 2011-12-10T16:15:56.937 回答