1

I want to have a QDirModel that contains both folders and files (so I am using AllEntries).

The problem is that when I am calling setNameFilters() on my QDirModel, I'll loose all the folders. Is there any way that I can exclude folders from filtering?

Thank you.

zx485
  • 28,498
  • 28
  • 50
  • 59
willy
  • 487
  • 5
  • 21

1 Answers1

3

You should use QFileSystemModel instead QDirModel:

The usage of QDirModel is not recommended anymore. The QFileSystemModel class is a more performant alternative.

Qt documentation: QDirModel and QFileSystemModel

For your question - look for combination of flags: QDir::AllDirs | QDir::Files in setFilter

Simple example, if you use QDirModel:

QTreeView view;
QStringList filters;
filters << "*.txt";
QDirModel* dirModel = new QDirModel(&view);
dirModel->setFilter(QDir::AllDirs | QDir::Files | QDir::NoDotAndDotDot);
dirModel->setNameFilters(filters);
view.setModel(dirModel);
view.show();

Simple example, if you use QFileSystemModel:

QTreeView view;
StringList filters;
filters << "*.txt";
QFileSystemModel* fileSystemModel = new QFileSystemModel(&view);
fileSystemModel->setRootPath("C:/");
fileSystemModel->setFilter(QDir::AllDirs | QDir::Files | QDir::NoDotAndDotDot);
fileSystemModel->setNameFilterDisables(false);
fileSystemModel->setNameFilters(filters);
view.setModel(fileSystemModel);
view.show();
Kirill Chernikov
  • 1,387
  • 8
  • 21
  • thank you @Kirill Chernikov. But, the question remains.. How is possible to exclude folders from being filtered? – willy Jun 29 '16 at 13:40
  • 1
    As you can see in documentation: "QDir::AllDirs - List all directories; i.e. don't apply the filters to directory names." When you wiil set filters for name, this filters will not apply for directories (folders), but only for files. – Kirill Chernikov Jun 29 '16 at 16:04