4

I want to have the user be able to select multiple folders and then store the paths of those folders in a list.

How can i make that happen? My current QFileDialog looks like this:

str = QtGui.QFileDialog.getExistingDirectory(self, "Open Directory", /folder/subfolder, QtGui.QFileDialog.DontResolveSymlinks)

But of course, it only lets me select one folder. How can I change it to select multiple folders and return them in a list?

Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
Gambit2007
  • 3,260
  • 13
  • 46
  • 86

1 Answers1

8

As far as I know you can't do that with the native FileDialog. There, however, exists a workaround in which you don't use the native dialog:

file_dialog = QFileDialog()
file_dialog.setFileMode(QFileDialog.DirectoryOnly)
file_dialog.setOption(QFileDialog.DontUseNativeDialog, True)
file_view = file_dialog.findChild(QListView, 'listView')

# to make it possible to select multiple directories:
if file_view:
    file_view.setSelectionMode(QAbstractItemView.MultiSelection)
f_tree_view = file_dialog.findChild(QTreeView)
if f_tree_view:
    f_tree_view.setSelectionMode(QAbstractItemView.MultiSelection)

if file_dialog.exec():
    paths = file_dialog.selectedFiles():

This workaround is a bit clunky however, but it's the best solution I know of other than rolling your own custom dialog.

iSplasher
  • 441
  • 5
  • 7