can the Open File dialog be used to select a Folder?

QtOpenfiledialog

Qt Problem Overview


The "Browse For Folder" Windows dialog is very inconvenient because:

  • it has no Path box where I can paste the path I want (eg from Total Commander)
  • it always starts from the Desktop with everything closed

Is there a way to use the "Open File" dialog (which is much better) to select a Folder? Some flag or option or something?

Context: the calibre eLibrary manager which is written in Python and Qt.

It currently displays as on the left. I'd like it to display as on the right BrowserFileDialog

or even better, as the Open File dialog: OpenFileDialog

Qt Solutions


Solution 1 - Qt

You can try this one:

QString QFileDialog::getExistingDirectory ( QWidget * parent = 0, const QString & caption = QString(), const QString & dir = QString(), Options options = ShowDirsOnly ) [static]

This one is used to choose a directory, and will popup a dialog like you show at last.

Demo:

 QString dir = QFileDialog::getExistingDirectory(this, tr("Open Directory"),
                                             "/home",
                                             QFileDialog::ShowDirsOnly
                                             | QFileDialog::DontResolveSymlinks);

Solution 2 - Qt

You can set the file mode in QFileDialog to QFileDialog::Directory

see http://qt-project.org/doc/qt-5.0/qtwidgets/qfiledialog.html#FileMode-enum

Or You can use QFileDialog::setOption with value QFileDialog::ShowDirsOnly

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionVladimir AlexievView Question on Stackoverflow
Solution 1 - QtliuyanghejerryView Answer on Stackoverflow
Solution 2 - QtRanjithView Answer on Stackoverflow