PyQt-QFileDialog-直接浏览到文件夹? [英] PyQt - QFileDialog - directly browse to a folder?
问题描述
有什么方法可以使用QFileDialog直接浏览到文件夹吗?
Is there any way to directly browse to a folder using QFileDialog?
意思是在导航至目标文件夹时无需双击每个文件夹,而只需在某处输入路径或在Mac OS X上的Finder中使用热键(如Shift + Command + G)即可.
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.
谢谢!
(我的代码)
filter = "Wav File (*.wav)"
self._audio_file = QtGui.QFileDialog.getOpenFileName(self, "Audio File",
"/myfolder/folder", filter)
self._audio_file = str(self._audio_file)
推荐答案
如果使用静态QFileDialog
函数,则会得到 native 文件对话框,因此您将限于平台提供的功能.您可以查阅所用平台的文档,以查看所需功能是否可用.
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.
如果它不可用,则必须解决Qt的内置文件对话框,并添加自己的功能.对于您的特定用例,这应该很容易,因为内置对话框似乎已经具有您想要的.它有一个侧边栏,其中显示了一个地方"列表用户可以直接导航到.您可以这样设置自己的位置:
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]
这篇关于PyQt-QFileDialog-直接浏览到文件夹?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!