Ikac03
Ikac03

Reputation: 73

Hide filtered files with QFileSystemModel, setNameFilterDisables(false) hides everything

I am trying to create simple file filter for my viewer.
Idea is to show only files allowed by filter and hide others (not disable them).
Some useful answers were found here and here, and using those examples I wrote this simple code:

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);

However, it is not working as expected.
When using line with setNameFilterDisables(false), I get no files at all.
This is not expected behavior for me as I would expect that files that do not pass the filter,
are not going to be shown at all.
This is described in the documentation:

This property holds whether files that don't pass the name filter are hidden or disabled.

So, how to properly filter the files and display only filtered ones?

Upvotes: 3

Views: 1121

Answers (1)

svlasov
svlasov

Reputation: 10455

You forgot to tree set root index:

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

If you want to keep directories visible, along with the filtered files, use QDir::AllDirs flag intead of QDir::Dirs.

Upvotes: 2

Related Questions