8

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)
ekhumoro
  • 98,079
  • 17
  • 183
  • 279
YaronGh
  • 311
  • 2
  • 3
  • 11

5 Answers5

8

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]
ekhumoro
  • 98,079
  • 17
  • 183
  • 279
7

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 ''
kblst
  • 305
  • 5
  • 9
5

Use getExistingDirectory method instead:

from PyQt5.QtWidgets import QFileDialog

dialog = QFileDialog()
foo_dir = dialog.getExistingDirectory(self, 'Select an awesome directory')
print(foo_dir)
Giulio Caccin
  • 2,554
  • 6
  • 30
  • 47
user3160702
  • 79
  • 1
  • 2
3

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:

enter image description here

ospahiu
  • 3,187
  • 2
  • 10
  • 22
  • For some reason that's not working for me.. using: `QtGui.QFileDialog.getOpenFileName(self, "Load File", filter)`. I don't get that path textfield. `filter` is such a string i created to select a specific type of files. – YaronGh Aug 03 '16 at 14:40
  • And like i mentioned below, if i set my QFileDialog to open directories, and then enter a directory path in the default path text field that appears, it just selects that folder and returns, it doesn't navigate to it and "stay" there. – YaronGh Aug 03 '16 at 14:43
  • Perhaps post a screenshot of your dialog, and full code? – ospahiu Aug 03 '16 at 14:46
0

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()
BPL
  • 9,807
  • 7
  • 37
  • 90
  • Thanks but that's not really what i was looking for.. i meant that i want to let the user navigate to a specific directory (without double-clicking all the way to it) after the file dialog has already been opened. – YaronGh Aug 03 '16 at 14:31
  • Not sure if I understand correctly then, getOpenFileName includes already a navigation bar where you can type your path directly without using the mouse at all, is that what you want? – BPL Aug 03 '16 at 14:37
  • Yes, but if i set my QFileDialog to open directories, and then enter a directory path, it just selects it and returns, and doesn't navigate to it and "stay" there. – YaronGh Aug 03 '16 at 14:41
  • Mmm, I've tried both getOpenFileName and getExistingDirectory on windows and once i type my path in the navigation bar and press enter it doesn't close the dialog and stays there. Maybe on MacOsX that's not the default behaviour – BPL Aug 03 '16 at 14:46
  • Weird.. maybe it really isn't. – YaronGh Aug 03 '16 at 14:49