[Qt-interest] Force file extension with QFileDialog::getSaveFileName?

David Doria daviddoria at gmail.com
Tue Jun 21 18:05:01 CEST 2011


On Tue, Jun 21, 2011 at 11:52 AM, Eric Clark <eclark at ara.com> wrote:
> If you create your own dialog, you can use QFileDialog::setDefaultSuffix. But if you use the static functions, you will need to implement the logic on your own. It should be fairly easy to do though:
>
> QString fileName = QFileDialog::getSaveFileName(...);
> QFileInfo file(filename);
> if(file.suffix().isEmpty()) fileName += ".txt";
>
> The above code is an example of the logic to add the default extension ".txt".

Wow you guys are quick!

I tried this:

QFileDialog dialog(this);
dialog.setFilter(".png .bmp");
dialog.setDefaultSuffix(".png");
cout << dialog.getSaveFileName().toStdString();

I typed 'test' in the dialog box, and the output was '/path/test'. I
would have expected it to be /path.test.png, no?

David



More information about the Qt-interest-old mailing list