c++ - 在 QFileDialog::getSaveFileName 中指定默认扩展名

标签 c++ qt qfiledialog

在 Win32 函数 GetSaveFileName 中是否有等同于 OPENFILENAME 结构的 lpstrDefExt 成员?

这是来自 MSDN 的描述:

LPCTSTR lpstrDefExt

The default extension. GetOpenFileName and GetSaveFileName append this extension to the file name if the user fails to type an extension. This string can be any length, but only the first three characters are appended. The string should not contain a period (.). If this member is NULL and the user fails to type an extension, no extension is appended.

因此,如果 lpstrDefExt 设置为“txt”并且用户键入“myfile”而不是“myfile.txt”,该函数仍会返回“myfile.txt”。

最佳答案

编辑:如果这对您不起作用,请查看 the answer below通过 @user52366

如果指定,Qt 将从“selectedFilter”参数中提取默认扩展名。

这是一个例子:

QString filter = "Worksheet Files (*.abd)";
QString filePath = QFileDialog::getSaveFileName(GetQtMainFrame(), tr("Save Worksheet"), defaultDir, filter, &filter);

使用此代码时,如果用户未在对话框中指定文件扩展名,则 getSaveFileName() 方法将自动添加“.abd”文件扩展名。您可以在“qfiledialog_win.cpp”Qt 源文件中的 qt_win_get_save_file_name() 中看到它的实现。

不幸的是,这不适用于 getOpenFileName() 方法。

关于c++ - 在 QFileDialog::getSaveFileName 中指定默认扩展名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7234381/

相关文章:

c++ - 带有 MinGW 和 gcc 4.7.1 的 OpenCV 2.4.2

c++ - 素描圆柱体

c++ - 如何在 Qt 5 中使用 LibVLC

c++ - 为什么 Qt 不鼓励图像本地化?

c++ - Qt (Creator) 与 WinSocks (ws2_32)

c++ - 窗口模式 QFileDialog 在 exec() 之后将父级推到后台

android - Qt 安卓 : How to show the native file dialog?

c++ openCV 3.1 - 不能像教程中那样使用 Mat 或大多数 openCV 东西

c++ - vc++ 安装 libcurl

c++ - 有没有办法在 Linux 上使用 QFileDialog 自动向文件添加扩展名