5

I want to have a file dialog only allow directories, here's what I've been trying:

fileDialog = QtGui.QFileDialog()
fileDialog.setFileMode(QtGui.QFileDialog.ShowDirsOnly)
filename = fileDialog.getOpenFileName(self, 'Select USB Drive Location'))
Courtney Christensen
  • 9,165
  • 5
  • 47
  • 56
matt
  • 153
  • 3
  • 9

3 Answers3

6

This is an old question, I know, but perhaps this will help someone else.

Use this snippet inside the method called to display the file box:

dialog = QtGui.QFileDialog(self)
dialog.setFileMode(QtGui.QFileDialog.Directory)
dialog.setOption(QtGui.QFileDialog.ShowDirsOnly, True)

if dialog.exec_():
    for d in dialog.selectedFiles():
        print d
hashbang
  • 61
  • 1
  • 4
6

What I wanted is:

directory = QtGui.QFileDialog.getExistingDirectory(self, 'Select USB Drive Location')
Courtney Christensen
  • 9,165
  • 5
  • 47
  • 56
matt
  • 153
  • 3
  • 9
1

The Qt 4.6 docs for ShowDirsOnly says:

"Only show directories in the file dialog. By default both files and directories are shown. (Valid only in the Directory file mode.)"

Maybe it isn't in "Directory" file mode?

WildCrustacean
  • 5,896
  • 2
  • 31
  • 42