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

Eric Clark eclark at ara.com
Tue Jun 21 18:33:05 CEST 2011



> -----Original Message-----
> From: David Doria [mailto:daviddoria at gmail.com]
> Sent: Tuesday, June 21, 2011 11:05 AM
> To: Eric Clark
> Cc: Qt Interest (qt-interest at trolltech.com)
> Subject: Re: [Qt-interest] Force file extension with
> QFileDialog::getSaveFileName?
> 
> 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");

Change this to:

dialog.setNameFilter(".png;;.bmp");

> dialog.setDefaultSuffix(".png");

Change this to:

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?

See if the changes I suggested give you the desired result.

Eric

> 
> David



More information about the Qt-interest-old mailing list