QFileDialog中的多个文件和文件夹选择(重新访问) [英] Multiple files and folder selection in QFileDialog (Revisited)

查看:565
本文介绍了QFileDialog中的多个文件和文件夹选择(重新访问)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此问题已经存在此处,但答案是从Python 2.7开始似乎已经过时或不再起作用.

This question already exists here, but the answer appears outdated or no longer works as of Python 2.7.

当我使用此代码对我的QFileDialog进行子类化时,将调用FileDialog类的init,但永远不会调用openClicked方法.

When I use this code to subclass my QFileDialog, the FileDialog class init is called, but the openClicked method is never called.

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

这是FileDialog的正确调用吗?

Is this the correct call for FileDialog?

mydialog = FileDialog()
filelist = mydialog.getExistingDirectory(self, "Select Stuff", "", QtGui.QFileDialog.DontConfirmOverwrite) 

推荐答案

另一个问题的解决方案不必要地复杂.您所需要做的就是覆盖QFileDialog.accept(),然后您可以自由实现所需的任何行为.

The solution from the other question is unnecessarily complicated. All you need to do is override QFileDialog.accept(), and then you are free to implement whatever behaviour you like.

以下示例非常少.它不会对所选文件进行任何检查(例如查看它们是否仍然存在),但是可以根据需要轻松添加.

The example below is very minimal. It doesn't do any checking of the selected files (like seeing whether they still exist), but that could easily be added if necessary.

from PyQt4 import QtCore, QtGui

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

    def accept(self):
        super(FileDialog, self).accept()

class Window(QtGui.QWidget):
    def __init__(self):
        super(Window, self).__init__()
        self.button = QtGui.QPushButton('Test', self)
        self.button.clicked.connect(self.handleButton)
        layout = QtGui.QVBoxLayout(self)
        layout.addWidget(self.button)

    def handleButton(self):
        dialog = FileDialog()
        if dialog.exec_() == QtGui.QDialog.Accepted:
            print(dialog.selectedFiles())

if __name__ == '__main__':

    import sys
    app = QtGui.QApplication(sys.argv)
    window = Window()
    window.show()
    sys.exit(app.exec_())

这篇关于QFileDialog中的多个文件和文件夹选择(重新访问)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆