matt
matt

Reputation: 153

Making File Dialog only accept directories

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'))

Upvotes: 5

Views: 6297

Answers (3)

hashbang
hashbang

Reputation: 61

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

Upvotes: 6

matt
matt

Reputation: 153

What I wanted is:

directory = QtGui.QFileDialog.getExistingDirectory(self, 'Select USB Drive Location')

Upvotes: 6

WildCrustacean
WildCrustacean

Reputation: 5966

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?

Upvotes: 1

Related Questions