使用 QFileDialog 时如何找出用户选择的后缀?
好吧,我正在使用以下代码来获取需要存储的文件的文件名。
QString fileName = QFileDialog::getSaveFileName(this, tr("Save File"),"/home/user/MyDocs/",tr("JPG files (*.jpg);;BMP files (*.bmp);;PNG files (*.png)"));
我为用户提供了许多有关要保存文件的文件格式的选项。但是,返回的 QString 只给我用户选择的前缀文件名,而不是后缀,因此我不知道用户选择哪种文件格式。如何检测这样的文件格式?
Well I'm using the following code to get the filename for a file that needs to be stored ..
QString fileName = QFileDialog::getSaveFileName(this, tr("Save File"),"/home/user/MyDocs/",tr("JPG files (*.jpg);;BMP files (*.bmp);;PNG files (*.png)"));
I'm providing the user with a number of options regarding the file format in which the file is to be saved. However, the returned QString
only gives me the prefix filename the user have chosen, not the suffix and thus I don't know which file format the user chose. How can I detect such a file format?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
问题中的代码适用于 Windows(Qt 4.6.2 和 Win XP)。 fileName 包含选定的扩展名。但是您显然正在使用其他 Windows,因此您可以尝试以下解决方法:
这是来自 此处。
The code in the question works in Windows (Qt 4.6.2 and Win XP). fileName contains the selected extension. But you are obviously using something else Windows, so you could try this workaround:
That is a slighty modified code from here.
您需要使用第五个可选字符串
我通常这样做:
编译器会小心地连接参数中的文字字符串。
我不确定返回的字符串如何与 tr() 交互。您必须进行测试并找出答案。可能需要取消翻译。
如果该函数能够返回所选过滤器的索引,那就更好了,但遗憾的是,它没有。
更好的解决方案是将过滤器放入列表中,从中创建一个字符串,然后将返回的选定过滤器字符串与列表中的过滤器字符串进行比较。这也可以解决
tr()
问题。You need to use the 5th optional string
I usually do it like this:
The compiler takes care to concatenate the literal strings in the argument.
I'm not sure how the returned string interacts with
tr()
. You'll have to test and find out. probably need to un-translate it.It could have been nicer if the function would return the index of the selected filter but alas, it does not.
A nicer solution would be to put the filters in a list, create a string from it and then compare to the returned selected filter string to the ones in the list. This would also solve the
tr()
problem.查看此讨论。它对在
QFileDialog
中输入的字符串使用QFileInfo
。Have a look to this discussion. It uses
QFileInfo
on the string that was entered in aQFileDialog
.