[PyQt] Tree View QFileDialog?

David Boddie dboddie at trolltech.com
Wed Jul 8 13:23:13 BST 2009


On Wed Jul 8 00:39:27 BST 2009, Brent Villalobos wrote:

> Perhaps I'm missing this, but is there a way to get a directory chooser
> dialog that shows the directories in a tree view?  I've been using
> "QtGui.QFileDialog.getExistingDirectory" as a directory-only selector,
> but I don't have an option to show the directories in a tree view.  I
> know I've used applications that show directories in a tree (where you
> can expand/contract nodes) and I was just wondering if Qt supports it
> out of the box as well and I'm just not seeing it.

As Andreas points out in his post, 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:

fd = QFileDialog()
tree = fd.findChild(QTreeView)
tree.setRootIsDecorated(True)
tree.setItemsExpandable(True)
fd.setFileMode(QFileDialog.Directory)
fd.setViewMode(QFileDialog.Detail)
result = fd.exec_()
if result:
  directory = fd.selectedFiles()[0]

Beware: if you double click on a directory, the dialog will navigate into it.

David


More information about the PyQt mailing list