Darin Beaudreau
Darin Beaudreau

Reputation: 391

Filtering directories displayed in a QFileDialog

I have an existing QFileDialog using the getExistingDirectory method that I'm trying to modify to filter out certain directories. Right now, all directories at the target location are shown, but I'm going to have a list/array of directory names that I need to filter for (ie: I want to show only these directories, not others).

Right now, this is how the file dialog is created:

QString dirpath = MvsFileDialog::getExistingDirectory(result_code,
                                                          this,
                                                          tr("Select Training"),        //title
                                                          tr("Library from list"), //subtitle
                                                          library,       //starting dir
                                                          QFileDialog::ShowDirsOnly | QFileDialog::DontResolveSymlinks); //options

The MvsFileDialog class is just an extension of the QFileDialog class.

How would I go about modifying this method call to filter FOR the directories I have in my list?

Upvotes: 0

Views: 259

Answers (1)

Lassi Heikkilä
Lassi Heikkilä

Reputation: 26

Have you checked if setting a name filter on the QFileDialog will work?

https://doc.qt.io/qt-5/qfiledialog.html#setNameFilter

Something like:

auto dialog = QFileDialog(parent, tr("Select Training"), library, "dir1;dir2");
dialog.setFileMode(QFileDialog::Directory);
dialog.setOption(QFileDialog::ShowDirsOnly | QFileDialog::DontResolveSymlinks);

The 4th parameter in the constructor is the name filter, it can also be set with QFileDialog::setNameFilter(const QString &filter);

Upvotes: 1

Related Questions