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'))
What I wanted is:
directory = QtGui.QFileDialog.getExistingDirectory(self, 'Select USB Drive Location')
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
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?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With