QFileDialog: adding extension automatically when saving file?


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.

QFileDialog fileDialog(this, "Choose file to save");
fileDialog.setNameFilter("PDF-Files (*.pdf)");
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?

12/23/2009 4:01:10 PM

Accepted Answer

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).

7/22/2013 7:32:15 PM

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow