Question

With PyKDE4.kio to select multiple files I can use KFileDialog.getOpenFileNames (instead of KFileDialog.getOpenFileName). What can I do if I want to select multiple directories? There is only KFileDialog.getExistingDirectory.

Using KFileDialog.getOpenFileNames(filter = 'inode/directory') and select multiple folders shows an error:

More than one folder has been selected and this dialog does not accept folders, so it is not possible to decide which one to enter. Please select only one folder to list it.

Was it helpful?

Solution

I found a solution from JohannesMunk on qtcentre.org and translated it to python

import sys
from PyQt5.QtWidgets import (QFileDialog, QAbstractItemView, QListView,
                             QTreeView, QApplication, QDialog)

class getExistingDirectories(QFileDialog):
    def __init__(self, *args):
        super(getExistingDirectories, self).__init__(*args)
        self.setOption(self.DontUseNativeDialog, True)
        self.setFileMode(self.Directory)
        self.setOption(self.ShowDirsOnly, True)
        self.findChildren(QListView)[0].setSelectionMode(QAbstractItemView.ExtendedSelection)
        self.findChildren(QTreeView)[0].setSelectionMode(QAbstractItemView.ExtendedSelection)

qapp = QApplication(sys.argv)
dlg = getExistingDirectories()
if dlg.exec_() == QDialog.Accepted:
    print(dlg.selectedFiles())
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top