如何使用KFileDialog选择多个目录?

时间:2013-09-09 21:23:41

标签: python linux qt kde pykde

使用PyKDE4.kio选择多个文件,我可以使用KFileDialog.getOpenFileNames(而不是KFileDialog.getOpenFileName)。如果我想选择多个目录,我该怎么办?只有KFileDialog.getExistingDirectory

使用KFileDialog.getOpenFileNames(filter = 'inode/directory')并选择多个文件夹会显示错误:

  

已选择多个文件夹,此对话框不接受文件夹,因此无法决定输入哪个文件夹。请只选择一个文件夹列出它。

1 个答案:

答案 0 :(得分:3)

我找到了JohannesMunk on qtcentre.org的解决方案并将其翻译为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())