QFileDialog中有多个文件和文件夹选择? [英] Multiple files AND folder selection in a QFileDialog?
问题描述
我正在使用pyQt4,并希望在GUI中有一个浏览按钮,该按钮会打开一个对话框,允许用户选择多个文件 AND 文件夹. 我已经研究了很多,但找不到任何方法可以做到这一点.
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.
QFileDialog.getOpenFileNames()仅允许我选择文件,而QFileDialog.getExistingDirectory()仅允许选择目录.
The QFileDialog.getOpenFileNames() only allows me to choose files and QFileDialog.getExistingDirectory() only allows to choose directories.
有什么办法可以以某种方式组合它们的功能. 理想情况下,我想使用nativeDialogs,但这似乎是不可能的. 因此,我愿意在外观上做出让步.我有什么办法可以实现所说的?
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?
这里也有人问过同样的问题,但是答案是c ++.我需要一个python实现. 允许用户在其中选择文件或文件夹QFileDialog
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
推荐答案
以下是对您有用的技巧:创建QFileDialog的子类,以断开打开"按钮的连接并将其重新连接到自定义函数.但是,不能保证它可以在不同版本的Qt上使用,因为它依赖于能够找到可以在某个时候重新配置的特定子窗口部件.
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
这篇关于QFileDialog中有多个文件和文件夹选择?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!