简体   繁体   English

PyQt QFileDialog - 多目录选择

[英]PyQt QFileDialog - Multiple Directory Selection

I am trying to create a QFileDialog that allows the user to select multiple directories. 我正在尝试创建一个允许用户选择多个目录的QFileDialog。

Following the discussion here and the faq here , but I'm not sure what I'm doing wrong. 这里讨论和常见问题之后 ,但我不确定我做错了什么。 I get a file dialog, but it still only lets me select a single directory (folder). 我得到一个文件对话框,但它仍然只允许我选择一个目录(文件夹)。

This is on Windows 7 这是在Windows 7上

Code: 码:

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

            self.tree = self.findChild(QtGui.QTreeView)
            self.tree.setSelectionMode(QtGui.QAbstractItemView.MultiSelection)

            self.list = self.findChild(QtGui.QListView)
            self.list.setSelectionMode(QtGui.QAbstractItemView.MultiSelection)

if __name__ == '__main__':
    import sys
    app = QtGui.QApplication(sys.argv)
    ex = FileDialog()
    ex.show()
    sys.exit(app.exec_())

Edit: 编辑:

So after playing around with this some more, if I select "Detail View" in the file dialog, multiselection works. 所以在玩了这个之后,如果我在文件对话框中选择“详细视图”,多选工作。 However, if I select "List View" it does not work. 但是,如果我选择“列表视图”它不起作用。 Any idea why? 知道为什么吗?

The example code from the FAQ is not robust, because it assumes the dialog only has one QListView and one QTreeView . FAQ中的示例代码不健壮,因为它假定对话框只有一个QListView和一个QTreeView The behaviour of findChild is indeterminate when there are several direct child objects: so it was probably just pure luck that it ever worked. 当有几个直接子对象时, findChild的行为是不确定的:所以它可能只是纯粹的运气它曾经工作过。

A more robust solution would be to reset the selection mode on any view for which the type of its model is a QFileSystemModel : 更强大的解决方案是在其模型类型为QFileSystemModel任何视图上重置选择模式:

for view in self.findChildren((QtGui.QListView, QtGui.QTreeView)):
    if isinstance(view.model(), QtGui.QFileSystemModel):
        view.setSelectionMode(QtGui.QAbstractItemView.MultiSelection)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM