1

However I found an older discussion about the subject here, it is not clear for me how to get the paths of the selected folders...

The combined codes from the source mentioned above is the following...

class FileDialog(QtGui.QFileDialog):
    def __init__(self, *args):
        QtGui.QFileDialog.__init__(self, *args)
        self.setOption(self.DontUseNativeDialog, True)
        self.setFileMode(self.DirectoryOnly)

        for view in self.findChildren((QtGui.QListView, QtGui.QTreeView)):
            if isinstance(view.model(), QtGui.QFileSystemModel):
                view.setSelectionMode(QtGui.QAbstractItemView.ExtendedSelection)

if __name__ == '__main__':
    import sys
    app = QtGui.QApplication(sys.argv)
    ex = FileDialog()
    ex.show()
    sys.exit(app.exec_())

which is able to select the folder, bit I do not know how to store them in a container.

The selection of files it is easy...like

fileNames = QtGui.QFileDialog.getOpenFileNames(self,'Open Images','',selectedFilter='*.dcm')

I would like to use something like this, can you help me?...sorry for the thumb question.

Community
  • 1
  • 1
Kristan
  • 387
  • 6
  • 19
  • `ex.exec_(); print(ex.selectedFiles())`. – ekhumoro Feb 03 '17 at 19:18
  • It works, now the program stops when I choose the directories and prints their paths in a list form. Just anothe thing...could you please comment why is there a "u" before the directory path in the list? I mean when I print it I got this: [u 'c:/....', u'c:/....'] – Kristan Feb 04 '17 at 08:22
  • 1
    The `u` indicates they are unicode strings rather than byte strings (in Python 2). – ekhumoro Feb 04 '17 at 18:35

1 Answers1

0

I don't understand how y'all got it working. This is the code I tried, with the imports included. However, after outputting the selected folders, it hangs, which means something is wrong.

from PyQt5 import QtGui
from PyQt5 import QtWidgets

class FileDialog(QtWidgets.QFileDialog):
    def __init__(self, *args):
        QtWidgets.QFileDialog.__init__(self, *args)
        self.setOption(self.DontUseNativeDialog, True)
        self.setFileMode(self.DirectoryOnly)

        for view in self.findChildren((QtWidgets.QListView, QtWidgets.QTreeView)):
            if isinstance(view.model(), QtWidgets.QFileSystemModel):
                view.setSelectionMode(QtWidgets.QAbstractItemView.ExtendedSelection)

if __name__ == '__main__':
    import sys
    app = QtWidgets.QApplication(sys.argv)
    ex = FileDialog()
    ex.show()
    ex.exec_()
    print(ex.selectedFiles())
    sys.exit(app.exec_())

There's an updated example provided by ekhumuro on this page.

Nav
  • 19,885
  • 27
  • 92
  • 135