C++ 如何在 QFileDialog 上设置选定的过滤器?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1604440/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 20:35:11  来源:igfitidea点击:

How to set selected filter on QFileDialog?

c++qtqt4

提问by Greg K

I have a open file dialog with three filters:

我有一个带有三个过滤器的打开文件对话框:

QString fileName = QFileDialog::getOpenFileName(
        this,
        title,
        directory,
        tr("JPEG (*.jpg *.jpeg);; TIFF (*.tif);; All files (*.*)")
);

This displays a dialog with "JPEG" selected as the default filter. I wanted to put the filter list in alphabetical order so "All files" was first in the list. If I do this however, "All files" is the default selected filter - which I don't want.

这将显示一个对话框,其中选择了“JPEG”作为默认过滤器。我想按字母顺序排列过滤器列表,以便“所有文件”在列表中排在第一位。但是,如果我这样做,“所有文件”是默认选择的过滤器 - 我不想要。

Can I set the default selected filter for this dialog or do I have to go with the first specified filter?

我可以为此对话框设置默认选择的过滤器还是必须使用第一个指定的过滤器?

I tried specifying a 5th argument (QString) to set the default selected filter but this didn't work. I think this might only be used to retrieve the filter that was set by the user.

我尝试指定第 5 个参数 ( QString) 来设置默认选择的过滤器,但这不起作用。我认为这可能仅用于检索用户设置的过滤器。

回答by shoosh

Like this:

像这样:

QString selfilter = tr("JPEG (*.jpg *.jpeg)");
QString fileName = QFileDialog::getOpenFileName(
        this,
        title,
        directory,
        tr("All files (*.*);;JPEG (*.jpg *.jpeg);;TIFF (*.tif)" ),
        &selfilter 
);

The docsare a bit vague about this, so I found this out via guessing.

文档是有点模糊这一点,所以我猜测通过发现了这一点。