简体   繁体   English

如何使用 QFileDialog 仅打开目录

[英]How to open only the directories with QFileDialog

This is my code:这是我的代码:

    def open(self):
        file_dialog = QtWidgets.QFileDialog(self)
        file_dialog.setMimeTypeFilters(["dir"])

I want to open only the directories, I use the setMimeTypeFilters method, it works well when I set parameters like "image/jpeg" or "video/mp4".我只想打开目录,我使用setMimeTypeFilters方法,当我设置“image/jpeg”或“video/mp4”等参数时效果很好。

There's actually a method built into QFileDialog that simplifies the process of getting directories called getExistingDirectory() .实际上,QFileDialog 中内置了一个方法,它简化了获取目录的过程,称为getExistingDirectory() Common usage would look something like:常见用法如下所示:

directory = QtWidgets.QFileDialog.getExistingDirectory(caption='caption', directory='C:\\path\\to\\starting\\directory')

It directly returns the selected directory as a string, or None if the dialog was cancelled.它直接将所选目录作为字符串返回,如果对话框被取消,则返回 None。 If you're looking for more control over your dialog, this may not be the best option.如果您正在寻找对对话的更多控制,这可能不是最佳选择。 Otherwise, this makes the process quite straightforward.否则,这会使该过程非常简单。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM