Search code examples
qtfolderbrowserdialog

Folder browser dialog in Qt


Is there any way to open a folder browser dialog in Qt? When I use QFileDialog with Directory file mode, even if I specify the ShowDirsOnly option, I get the standard file dialog. I would prefer to use a dialog that asks the user to choose a directory from a directory tree.

Here's the PySide code I'm using:

from PySide import QtGui
app = QtGui.QApplication([])
dialog = QtGui.QFileDialog()
dialog.setFileMode(QtGui.QFileDialog.Directory)
dialog.setOption(QtGui.QFileDialog.ShowDirsOnly)
dialog.exec_()

And here's the result I get on Windows 7: File selection dialog


Solution

  • It appears that the order in which you call setFileMode() and setOption() matters. Make sure you're calling setFileMode() first:

    QFileDialog dialog;
    dialog.setFileMode(QFileDialog::Directory);
    dialog.setOption(QFileDialog::ShowDirsOnly);
    ...