使用 QFileDialog 时如何找出用户选择的后缀?

发布于 2024-09-08 03:35:54 字数 313 浏览 9 评论 0原文

好吧,我正在使用以下代码来获取需要存储的文件的文件名。

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

极致的悲 2024-09-15 03:35:54

问题中的代码适用于 Windows(Qt 4.6.2 和 Win XP)。 fileName 包含选定的扩展名。但是您显然正在使用其他 Windows,因此您可以尝试以下解决方法:

QFileDialog dialog(this, tr("Save as ..."), "/home/user/MyDocs/");
dialog.setAcceptMode(QFileDialog::AcceptSave);
QStringList filters;
filters << "JPG files (*.jpg)" << "BMP files (*.bmp)" << "PNG files (*.png)";
dialog.setNameFilters(filters);
if (dialog.exec() == QDialog::Accepted)
{
    QString selectedFilter = dialog.selectedNameFilter();
    QString fileName = dialog.selectedFiles()[0];
}

这是来自 此处

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:

QFileDialog dialog(this, tr("Save as ..."), "/home/user/MyDocs/");
dialog.setAcceptMode(QFileDialog::AcceptSave);
QStringList filters;
filters << "JPG files (*.jpg)" << "BMP files (*.bmp)" << "PNG files (*.png)";
dialog.setNameFilters(filters);
if (dialog.exec() == QDialog::Accepted)
{
    QString selectedFilter = dialog.selectedNameFilter();
    QString fileName = dialog.selectedFiles()[0];
}

That is a slighty modified code from here.

魂牵梦绕锁你心扉 2024-09-15 03:35:54

您需要使用第五个可选字符串
我通常这样做:

#define JPEG_FILES "JPG files (*.jpg)"
#define BMP_FILES "BMP files (*.bmp)"
#define PNG_FILES "PNG files (*.png)"

QString selectedFilter;
QString fileName = QFileDialog::getSaveFileName(this, tr("Save File"),
        "/home/user/MyDocs/",
        JPEG_FILES ";;" BMP_FILES ";;" PNG_FILES, &selectedFilter);

if (fileName.isNull())
  return;
if (selectedFilter == JPEG_FILES) {
  ...
} else if (selectedFilter == BMP_FILES) { 
  ...
} else if (selectedFilter == PNG_FILES) {
  ...
} else {  
    // something strange happened 
}

编译器会小心地连接参数中的文字字符串。

我不确定返回的字符串如何与 tr() 交互。您必须进行测试并找出答案。可能需要取消翻译。
如果该函数能够返回所选过滤器的索引,那就更好了,但遗憾的是,它没有。

更好的解决方案是将过滤器放入列表中,从中创建一个字符串,然后将返回的选定过滤器字符串与列表中的过滤器字符串进行比较。这也可以解决 tr() 问题。

You need to use the 5th optional string
I usually do it like this:

#define JPEG_FILES "JPG files (*.jpg)"
#define BMP_FILES "BMP files (*.bmp)"
#define PNG_FILES "PNG files (*.png)"

QString selectedFilter;
QString fileName = QFileDialog::getSaveFileName(this, tr("Save File"),
        "/home/user/MyDocs/",
        JPEG_FILES ";;" BMP_FILES ";;" PNG_FILES, &selectedFilter);

if (fileName.isNull())
  return;
if (selectedFilter == JPEG_FILES) {
  ...
} else if (selectedFilter == BMP_FILES) { 
  ...
} else if (selectedFilter == PNG_FILES) {
  ...
} else {  
    // something strange happened 
}

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.

狠疯拽 2024-09-15 03:35:54

查看讨论。它对在QFileDialog 中输入的字符串使用QFileInfo

Have a look to this discussion. It uses QFileInfo on the string that was entered in a QFileDialog.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文