python - 在 QFileDialog pyqt5 中选择文件或文件夹
问题描述
我的脚本目前使用 QtWidgets.QFileDialog.getOpenFileNames() 让用户在 Windows 资源管理器中选择文件。现在我想知道是否有办法让他们也选择文件夹,而不仅仅是文件。有一些类似的帖子,但没有一个提供有效的解决方案。我真的不想使用 QFileDialog 文件资源管理器来解决这个问题。
解决方案
QFileDialog 本机不允许这样做。唯一的解决方案是创建自己的实例,做一些小的“修补”。
请注意,为了实现这一点,您不能使用操作系统的本机对话框,因为 Qt 几乎无法控制它们;dialog.DontUseNativeDialog
这就是标志的原因,这是强制性的。
以下代码与静态方法一样工作,并返回所选项目(如果对话框被取消,则返回任何项目)。
def getOpenFilesAndDirs(parent=None, caption='', directory='',
filter='', initialFilter='', options=None):
def updateText():
# update the contents of the line edit widget with the selected files
selected = []
for index in view.selectionModel().selectedRows():
selected.append('"{}"'.format(index.data()))
lineEdit.setText(' '.join(selected))
dialog = QtWidgets.QFileDialog(parent, windowTitle=caption)
dialog.setFileMode(dialog.ExistingFiles)
if options:
dialog.setOptions(options)
dialog.setOption(dialog.DontUseNativeDialog, True)
if directory:
dialog.setDirectory(directory)
if filter:
dialog.setNameFilter(filter)
if initialFilter:
dialog.selectNameFilter(initialFilter)
# by default, if a directory is opened in file listing mode,
# QFileDialog.accept() shows the contents of that directory, but we
# need to be able to "open" directories as we can do with files, so we
# just override accept() with the default QDialog implementation which
# will just return exec_()
dialog.accept = lambda: QtWidgets.QDialog.accept(dialog)
# there are many item views in a non-native dialog, but the ones displaying
# the actual contents are created inside a QStackedWidget; they are a
# QTreeView and a QListView, and the tree is only used when the
# viewMode is set to QFileDialog.Details, which is not this case
stackedWidget = dialog.findChild(QtWidgets.QStackedWidget)
view = stackedWidget.findChild(QtWidgets.QListView)
view.selectionModel().selectionChanged.connect(updateText)
lineEdit = dialog.findChild(QtWidgets.QLineEdit)
# clear the line edit contents whenever the current directory changes
dialog.directoryEntered.connect(lambda: lineEdit.setText(''))
dialog.exec_()
return dialog.selectedFiles()
推荐阅读
- python - 我想抓取点击“加载更多参数”时显示的网站 JavaScript 加载数据
- python - Python - 如果另一个列表中存在任何子字符串,则返回字符串列表
- java - 评估表达式的递归方法仅在方法被调用一次时才起作用
- python - nfcpy 从 NFC 标签中检索 URL。但是如何打开链接?
- wordpress - 在wordpress的特定页面上隐藏菜单
- java - 带有 Oracle 数据源的 JBoss EAP 7
- python - 有人可以像我 5 岁那样向我解释这是做什么的吗?
- google-cloud-platform - Travis -Gcloud 崩溃(AttributeError):“_RSAPrivateKey”对象没有属性“sign”
- javascript - 节点ws服务器未接收到客户端信息
- nearprotocol - 赚取一定比例的智能合约产生的交易费用