简体   繁体   English

PyQt - QFileDialog - 直接浏览到一个文件夹?

[英]PyQt - QFileDialog - directly browse to a folder?

Is there any way to directly browse to a folder using QFileDialog? 有没有办法直接浏览到使用QFileDialog的文件夹?

Meaning, instead of double clicking on each folder while navigating to the destination folder, simply enter the path somewhere or use a hotkey like the one (Shift+Command+G) in Finder on Mac OS X. 这意味着,不是在导航到目标文件夹时双击每个文件夹,只需在某处输入路径或使用Mac OS X上的Finder中的热键(Shift + Command + G)。

Thanks! 谢谢!

EDIT: (my code) 编辑:(我的代码)

    filter = "Wav File (*.wav)"
    self._audio_file = QtGui.QFileDialog.getOpenFileName(self, "Audio File",
                                                        "/myfolder/folder", filter)
    self._audio_file = str(self._audio_file)

If you use the static QFileDialog functions, you'll get a native file-dialog, and so you'll be limited to the functionality provided by the platform. 如果使用静态QFileDialog函数,您将获得本机文件对话框,因此您将受限于平台提供的功能。 You can consult the documentation for your platform to see if the functionality you want is available. 您可以查阅适用于您的平台的文档,以了解您所需的功能是否可用。

If it's not available, you'll have to settle for Qt's built-in file-dialog, and add your own features. 如果它不可用,你将不得不接受Qt的内置文件对话框,并添加自己的功能。 For your specific use-case, this should be easy, because the built-in dialog already seems to have what you want. 对于您的特定用例,这应该很简单,因为内置对话框似乎已经拥有您想要的内容。 It has a side-bar that shows a list of "Places" that the user can navigate to directly. 它有一个侧栏 ,显示用户可以直接导航到的“位置”列表。 You can set your own places like this: 您可以像这样设置自己的地方:

dialog = QtGui.QFileDialog(self, 'Audio Files', directory, filter)
dialog.setFileMode(QtGui.QFileDialog.DirectoryOnly)
dialog.setSidebarUrls([QtCore.QUrl.fromLocalFile(place)])
if dialog.exec_() == QtGui.QDialog.Accepted:
    self._audio_file = dialog.selectedFiles()[0]

In PyQt 4 , you're able to just add a QFileDialog to construct a window that has a path textfield embedded inside of the dialog. PyQt 4 ,您只需添加一个QFileDialog来构造一个窗口,该窗口在对话框中嵌入了路径文本字段。 You can paste your path in here. 您可以在此处粘贴路径。

QtGui.QFileDialog.getOpenFileName(self, 'Select file')  # For file.

For selecting a directory: 用于选择目录:

QtGui.QFileDialog.getExistingDirectory(self, 'Select directory')

Each will feature a path textfield : 每个都将以路径文本字段为特色

在此输入图像描述

Here's a convenience function for quickly making an open/save QFileDialog . 这是一个方便的功能,可以快速打开/保存QFileDialog

from PyQt5.QtWidgets import QFileDialog, QDialog
from definitions import ROOT_DIR
from PyQt5 import QtCore


def FileDialog(directory='', forOpen=True, fmt='', isFolder=False):
    options = QFileDialog.Options()
    options |= QFileDialog.DontUseNativeDialog
    options |= QFileDialog.DontUseCustomDirectoryIcons
    dialog = QFileDialog()
    dialog.setOptions(options)

    dialog.setFilter(dialog.filter() | QtCore.QDir.Hidden)

    # ARE WE TALKING ABOUT FILES OR FOLDERS
    if isFolder:
        dialog.setFileMode(QFileDialog.DirectoryOnly)
    else:
        dialog.setFileMode(QFileDialog.AnyFile)
    # OPENING OR SAVING
    dialog.setAcceptMode(QFileDialog.AcceptOpen) if forOpen else dialog.setAcceptMode(QFileDialog.AcceptSave)

    # SET FORMAT, IF SPECIFIED
    if fmt != '' and isFolder is False:
        dialog.setDefaultSuffix(fmt)
        dialog.setNameFilters([f'{fmt} (*.{fmt})'])

    # SET THE STARTING DIRECTORY
    if directory != '':
        dialog.setDirectory(str(directory))
    else:
        dialog.setDirectory(str(ROOT_DIR))


    if dialog.exec_() == QDialog.Accepted:
        path = dialog.selectedFiles()[0]  # returns a list
        return path
    else:
        return ''

Below you'll find a simple test which opens directly the dialog at a certain path, in this case will be the current working directory. 下面你会发现一个简单的测试,它直接打开某个路径上的对话框,在这种情况下将是当前的工作目录。 If you want to open directly another path you can just use python's directory functions included in os.path module: 如果要直接打开另一个路径,可以使用os.path模块中包含的python目录函数:

 import sys
 import os
 from PyQt4 import QtGui


 def test():
      filename = QtGui.QFileDialog.getOpenFileName(
           None, 'Test Dialog', os.getcwd(), 'All Files(*.*)')

 def main():
     app = QtGui.QApplication(sys.argv)

     test()

     sys.exit(app.exec_())

 if __name__ == "__main__":
     main()

Use getExistingDirectory method instead: 改为使用getExistingDirectory方法:

from PyQt5.QtWidgets import QFileDialog

dialog = QFileDialog()
foo_dir = dialog.getExistingDirectory(self, 'Select an awesome directory')
print(foo_dir)

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

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