14

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

Vojislav Stojkovic
  • 8,043
  • 4
  • 35
  • 48

4 Answers4

12

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);
...
Chris
  • 17,119
  • 5
  • 57
  • 60
  • Thanks for the quick reply! Unfortunately, that's the order in which I'm doing it. I've edited my question to include the code snippet, the screenshot and the OS that I'm testing on. It would be great if anyone could point out what I'm doing wrong. And if I'm not doing anything wrong, it's nice to know that it's because of such-and-such factor ;) – Vojislav Stojkovic Nov 08 '12 at 23:25
  • Ah I see what you're trying to do now. It mignt not be possible to get exactly what you're looking for, as I think it's a somewhat Windows-specific style of dialog and QFileDialog is trying to be generic for all platforms. – Chris Nov 08 '12 at 23:43
  • I was afraid that would be the case, but hoped someone would point out something I did wrong instead ;) Thanks! – Vojislav Stojkovic Nov 09 '12 at 10:05
5

I know, that my answer is some tricky and looks like little hack, but the QFileDialog static methods like getExistingDirectory() use the native dialog, so only limited customization is possible.

However, if you create a QFileDialog instance, you get a dialog that can be customized -- as long as you're happy messing with a live dialog.

For example, this should show a tree view with expandable directories that you can select (hope, it must be not a problem port this code to PySide):

QFileDialog *fd = new QFileDialog;
QTreeView *tree = fd->findChild <QTreeView*>();
tree->setRootIsDecorated(true);
tree->setItemsExpandable(true);
fd->setFileMode(QFileDialog::Directory);
fd->setOption(QFileDialog::ShowDirsOnly);
fd->setViewMode(QFileDialog::Detail);
int result = fd->exec();
QString directory;
if (result)
{
    directory = fd->selectedFiles()[0];
    qDebug()<<directory;
}

Got that method from here

NG_
  • 6,895
  • 7
  • 45
  • 67
3

Try this line of code, it show you a folder browse dialog:

 ui->txtSaveAddress->setText(folderDlg.getExistingDirectory(0,"Caption",QString(),QFileDialog::ShowDirsOnly));

enter image description here

mesut
  • 2,099
  • 3
  • 23
  • 35
3

This worked for me:

def getDir(self):
    dialog = QtGui.QFileDialog()
    dialog.setFileMode(QtGui.QFileDialog.Directory)
    dialog.setOption(QtGui.QFileDialog.ShowDirsOnly)
    directory = dialog.getExistingDirectory(self, 'Choose Directory', os.path.curdir)
ForeverWintr
  • 5,492
  • 2
  • 36
  • 65