Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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'))
like image 897
matt Avatar asked Apr 18 '10 14:04

matt


3 Answers

What I wanted is:

directory = QtGui.QFileDialog.getExistingDirectory(self, 'Select USB Drive Location')
like image 161
matt Avatar answered Oct 17 '22 03:10

matt


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
like image 36
hashbang Avatar answered Oct 17 '22 01:10

hashbang


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?

like image 1
WildCrustacean Avatar answered Oct 17 '22 01:10

WildCrustacean