PyQt - QFileDialog - directly browse to a folder?

感情迁移 提交于 2019-12-22 10:22:49

问题


Is there any way to directly browse to a folder using 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.

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)

回答1:


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. 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. 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]



回答2:


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. 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:




回答3:


Here's a convenience function for quickly making an open/save 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 ''



回答4:


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:

 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()



回答5:


Use getExistingDirectory method instead:

from PyQt5.QtWidgets import QFileDialog

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


来源:https://stackoverflow.com/questions/38746002/pyqt-qfiledialog-directly-browse-to-a-folder

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!