Folder browser dialog in Qt
Asked Answered
C

4

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

Chamfer answered 8/11, 2012 at 22:50 Comment(0)
P
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);
...
Ponceau answered 8/11, 2012 at 23:7 Comment(3)
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 ;)Chamfer
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.Ponceau
I was afraid that would be the case, but hoped someone would point out something I did wrong instead ;) Thanks!Chamfer
C
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

Cudlip answered 9/11, 2012 at 9:16 Comment(0)
M
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

Mccraw answered 29/5, 2013 at 9:19 Comment(0)
M
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)
Monkey answered 19/11, 2014 at 21:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.