在 QFileDialog::getSaveFileName 中指定默认扩展名
是否有与 Win32 函数 GetSaveFileName 中使用的 OPENFILENAME 结构的 lpstrDefExt 成员等效的函数?
以下是来自 MSDN 的描述:
LPCTSTR lpstrDefExt
默认扩展名。 GetOpenFileName 和 GetSaveFileName 附加此内容 如果用户未能键入扩展名,则为文件名添加扩展名。 该字符串可以是任意长度,但只有前三个字符 附加。该字符串不应包含句点 (.)。如果这个会员 为 NULL 并且用户无法键入扩展名,没有扩展名 附后。
因此,如果 lpstrDefExt 设置为“txt”并且用户键入“myfile”而不是“myfile.txt”,则该函数仍返回“myfile.txt”。
Is there an equivalent of the lpstrDefExt member of OPENFILENAME struct used in the Win32 function GetSaveFileName?
Here's description from 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.
So if lpstrDefExt is set to "txt" and the user types "myfile" instead of "myfile.txt", the function still returns "myfile.txt".
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
编辑:如果这对您不起作用,请查看@user52366
Qt 的下面的答案从“selectedFilter”参数中提取默认扩展名(如果指定)。
下面是一个示例:
使用此代码时,如果用户未在对话框中指定文件扩展名,则 getSaveFileName() 方法将自动添加“.abd”文件扩展名。您可以在“qfiledialog_win.cpp”Qt 源文件内的 qt_win_get_save_file_name() 中看到其实现。
不幸的是,这不适用于 getOpenFileName() 方法。
Edit: If this does not work for you look at the answer below by @user52366
Qt will extract the default extension from the "selectedFilter" parameter, if specified.
Here is an example:
When using this code the getSaveFileName() method will automatically add the ".abd" file extension if the user didn't specify one in the dialog. You can see the implementation of this in the qt_win_get_save_file_name() inside the "qfiledialog_win.cpp" Qt source file.
Unfortunately this doesn't work for the getOpenFileName() method.
正如上面评论中提到的,这不起作用,至少对我来说。
最后我跳过了静态方法并使用了以下内容:
As mentioned in the comment above, this does not work, at least for me.
In the end I skipped the static method and used the following:
不确定 LPCTSTR lpstrDefExt 到底想做什么,但 Qt 文档给出了以下示例
http: //doc.qt.io/qt-5/qfiledialog.html#getSaveFileName
Not sure what exactly LPCTSTR lpstrDefExt is trying to do but Qt documentation gives the following example
http://doc.qt.io/qt-5/qfiledialog.html#getSaveFileName