I'm setting my QFileSystemModel with following filters:

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

In my proxy model, I am using a regular expression to filter files by name:

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

....and then where my model_ is a QFileSystemModel, I have the line:

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

...yet files displayed are greyed out. Why, and how to make them "normal".

有帮助吗?

解决方案

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!

其他提示

Actually, the format is inconsistent between different Qt classes. If they take a single QString, then it's as @HostileFork says. In this case, however, the function setNameFilters() takes a QStringList, which means you want:

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

Since your input was the wrong format (regex, instead of Window's wildcards), everything was marked as "filter this out" because nothing matched the weird syntax.

Why greyed out? Because QFileSystemModel by default disabled/greys-out files that get filtered (bwah?), instead of hiding them. This can be changed by calling:

fileModel->setNameFilterDisables(false);

QFileSystemModel's 'nameFilterDisables' property

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top