QFileDialog:保存文件时自动添加扩展名? [英] QFileDialog: adding extension automatically when saving file?

查看:3060
本文介绍了QFileDialog:保存文件时自动添加扩展名?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当使用 QFileDialog 保存文件并指定扩展名(如* .pdf)和用户在没有此扩展名的用户类型时,保存的文件没有此扩展名。

示例代码:

  QFileDialog文件对话框保存); 
fileDialog.setNameFilter(PDF-Files(* .pdf));
fileDialog.exec();
QFile pdfFile(fileDialog.selectedFiles()。first());

现在当用户输入foo作为名称时,文件将被保存为foo ,而不是foo.pdf。因此 QFileDialog 不会自动添加扩展名。我可以使用

org / doc / qt-5.0 / qtwidgets / qfiledialog.html#defaultSuffix-prop> QFileDialog :: setDefaultSuffix()


如果没有指定其他后缀,此属性会将后缀添加到文件名。



此属性指定一个字符串,如果它没有后缀,将被添加到文件名。后缀通常用于表示文件类型(例如txt表示文本文件)。



When using a QFileDialog to save a file and to specify the extension (like *.pdf) and the user types in a name without this extension, also the saved file hasn't this extension.
Example-Code:

QFileDialog fileDialog(this, "Choose file to save");
fileDialog.setNameFilter("PDF-Files (*.pdf)");
fileDialog.exec();
QFile pdfFile(fileDialog.selectedFiles().first());

now when the user enters "foo" as the name, the file will be saved as "foo", not as "foo.pdf". So the QFileDialog doesn't add the extension automatically. My question: How can I change this?

解决方案

You could use QFileDialog::setDefaultSuffix():

This property holds suffix added to the filename if no other suffix was specified.

This property specifies a string that will be added to the filename if it has no suffix already. The suffix is typically used to indicate the file type (e.g. "txt" indicates a text file).

这篇关于QFileDialog:保存文件时自动添加扩展名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆