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

标签 python linux qt kde-plasma pykde

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

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

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.

最佳答案

我从 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())

关于python - 如何使用 KFileDialog 选择多个目录?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18707261/

相关文章:

python - 在 Python DataFrame 中拆分字符串

python - 如何查看安装了哪些 Python 模块以及版本?

python - 快速查找最近更改的文件

qt - Qt 中未显示 JPEG 图像

c++ - 将 QtSoap 引用添加到 Qt Creator

Python 测试点是否在矩形中

python - 在 pygame 中暂停游戏和用户输入

linux - 我想在 linux 上删除多行文本

linux - 在获取 .bashrc 后,如何在使用 ssh 登录时在远程执行命令?

c++ - 是否可以根据其位置更改 QSlider handle 的颜色?