QFileDialog 中的多个文件和文件夹选择?

发布于 2024-11-17 03:30:35 字数 481 浏览 3 评论 0原文

我正在使用 pyQt4,希望在 GUI 中有一个浏览按钮,该按钮打开一个对话框,允许用户选择多个文件文件夹。 我已经研究了很多,但没有找到任何方法可以做到这一点。

QFileDialog.getOpenFileNames() 只允许我选择文件,而 QFileDialog.getExistingDirectory() 只允许选择目录。

有什么办法可以将它们的功能结合起来吗? 理想情况下,我想使用 nativeDialogs 但这似乎不可能。 因此,我愿意在外观上做出妥协。有什么办法可以实现我所说的吗?

同样的问题也在这里被问到,但答案是在 c++ 中。我需要一个 python 实现。 允许用户选择文件或文件夹QFileDialog

I am using pyQt4 and want to have a Browse button in my GUI which opens up a Dialog box allowing user to select multiple files AND folders.
I have researched quite a bit but din't find any way to be able to do this.

The QFileDialog.getOpenFileNames() only allows me to choose files and QFileDialog.getExistingDirectory() only allows to choose directories.

Is there any way I can somehow combine their functionality.
Ideally I would like to use the nativeDialogs but that doesn't seem to be possible.
As a result I am willing to compromise on the looks. Is there any way I can implement the said?

The same question has been asked here as well but the answer is in c++. I need a python implementation.
Allow user to select a file or a folder in QFileDialog

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

愛放△進行李 2024-11-24 03:30:35

这是一个应该适合您的技巧:创建 QFileDialog 的子类,断开“打开”按钮并将其重新连接到自定义函数。不过,它不能保证在不同版本的 Qt 上工作,因为它依赖于找到可能在某个时刻重新配置的特定子小部件。

class FileDialog(QtGui.QFileDialog):
    def __init__(self, *args):
        QtGui.QFileDialog.__init__(self, *args)
        self.setOption(self.DontUseNativeDialog, True)
        self.setFileMode(self.ExistingFiles)
        btns = self.findChildren(QtGui.QPushButton)
        self.openBtn = [x for x in btns if 'open' in str(x.text()).lower()][0]
        self.openBtn.clicked.disconnect()
        self.openBtn.clicked.connect(self.openClicked)
        self.tree = self.findChild(QtGui.QTreeView)

    def openClicked(self):
        inds = self.tree.selectionModel().selectedIndexes()
        files = []
        for i in inds:
            if i.column() == 0:
                files.append(os.path.join(str(self.directory().absolutePath()),str(i.data().toString())))
        self.selectedFiles = files
        self.hide()

    def filesSelected(self):
        return self.selectedFiles

Here's a hack that should work for you: Create a subclass of QFileDialog that disconnects the "Open" button and reconnects it to a customized function. It's not guaranteed to work across different versions of Qt, though, since it relies on being able to find specific sub-widgets that may be reconfigured at some point.

class FileDialog(QtGui.QFileDialog):
    def __init__(self, *args):
        QtGui.QFileDialog.__init__(self, *args)
        self.setOption(self.DontUseNativeDialog, True)
        self.setFileMode(self.ExistingFiles)
        btns = self.findChildren(QtGui.QPushButton)
        self.openBtn = [x for x in btns if 'open' in str(x.text()).lower()][0]
        self.openBtn.clicked.disconnect()
        self.openBtn.clicked.connect(self.openClicked)
        self.tree = self.findChild(QtGui.QTreeView)

    def openClicked(self):
        inds = self.tree.selectionModel().selectedIndexes()
        files = []
        for i in inds:
            if i.column() == 0:
                files.append(os.path.join(str(self.directory().absolutePath()),str(i.data().toString())))
        self.selectedFiles = files
        self.hide()

    def filesSelected(self):
        return self.selectedFiles
网名女生简单气质 2024-11-24 03:30:35

在 Qt5 中你可以简单地使用

return QtWidgets.QFileDialog.getOpenFileNames(self, title, directory, filter=filterFile)

In Qt5 you can simply use

return QtWidgets.QFileDialog.getOpenFileNames(self, title, directory, filter=filterFile)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文