如何在QFileDialog上设置选定的过滤器? [英] How to set selected filter on QFileDialog?
问题描述
我有一个打开的文件对话框,有三个过滤器:
I have a open file dialog with three filters:
QString fileName = QFileDialog::getOpenFileName(
this,
title,
directory,
tr("JPEG (*.jpg *.jpeg);; TIFF (*.tif);; All files (*.*)")
);
这将显示一个对话框,其中选择JPEG作为默认过滤器。我想把过滤器列表按字母顺序排列,所以所有文件是列表中的第一个。如果我这样做,然而,所有文件是默认选择的过滤器,我不想要的。
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.
我可以设置默认选择过滤器,
Can I set the default selected filter for this dialog or do I have to go with the first specified filter?
我尝试指定第五个参数( QString
)来设置默认选择过滤器,但这没有工作。我认为这可能只用于检索由用户设置的过滤器。
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.
推荐答案
Like this:
QString selfilter = tr("JPEG (*.jpg *.jpeg)");
QString fileName = QFileDialog::getOpenFileName(
this,
title,
directory,
tr("All files (*.*);;JPEG (*.jpg *.jpeg);;TIFF (*.tif)" ),
&selfilter
);
我同意文档:
http://doc.qt.digia.com/3.3/qfiledialog.html#getOpenFileName
is abit对这一点不清楚,但它只花了我一个尝试猜测如何做到这一点。
I concur that the docs:
http://doc.qt.digia.com/3.3/qfiledialog.html#getOpenFileName
are abit vague about this point but it only took me one try to guess how to do this right.
这是一个案件,它是更快,只是试一试, 。
This is one case where it's faster to just try it than to ask a question.
这篇关于如何在QFileDialog上设置选定的过滤器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!