PyQt - QFileDialog - directly browse to a folder?
Solution 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]
Solution 2
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 ''
Solution 3
Use getExistingDirectory
method instead:
from PyQt5.QtWidgets import QFileDialog
dialog = QFileDialog()
foo_dir = dialog.getExistingDirectory(self, 'Select an awesome directory')
print(foo_dir)
Solution 4
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:
YaronGh
Updated on August 25, 2020Comments
-
YaronGh almost 4 years
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)
-
YaronGh almost 8 yearsThanks 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.
-
BPL almost 8 yearsNot 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?
-
YaronGh almost 8 yearsFor 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 almost 8 yearsYes, 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 almost 8 yearsAnd 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.
-
ospahiu almost 8 yearsPerhaps post a screenshot of your dialog, and full code?
-
BPL almost 8 yearsMmm, 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
-
YaronGh almost 8 yearsWeird.. maybe it really isn't.
-
YaronGh almost 8 yearsGreat, the
Sidebar
thing definitely helps. Thanks! -
Oscar Lima almost 5 yearsRight (just noticed it now). Only difference would be the PyQt version (4 vs 5)
-
gabriel garcia over 3 yearsIs
ROOT_DIR
=os.path.abspath(__file__)
? What is thatdefinitions
module?