Search code examples
pythonqtuser-interfacepyqtqfiledialog

PyQt - QFileDialog - directly browse to a folder?


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)

Solution

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