Question

How can I set QFileSystemModel to display just directories?
Or is this impossible at all?

Was it helpful?

Solution

Have never had a chance to use the QFileSystemModel myself, but looking at the docs...did you try setFilter()?

http://doc.qt.io/qt-5/qfilesystemmodel.html#setFilter

http://doc.qt.io/qt-5/qdir.html#Filter-enum

The documentation notes, the default filter is QDir::AllEntries | QDir::NoDotAndDotDot | QDir::AllDirs. That expands to: QDir::Dirs | QDir::Files | QDir::Drives | QDir::NoDotAndDot | QDir::AllDirs, and it says AllDirs is required.

So maybe just:

model->setFilter(QDir::Dirs|QDir::Drives|QDir::NoDotAndDotDot|QDir::AllDirs);

OTHER TIPS

In one working project in my hand, another approach (not QFileSystemModel) is taken to display only directories:

QFileDialog::Options options = QFileDialog::DontResolveSymlinks | QFileDialog::ShowDirsOnly;
options |= QFileDialog::DontUseNativeDialog;
QString directory = QFileDialog::getExistingDirectory(this,
    tr("Select the data path"),
    "",
    options);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top