[Qt-interest] Force file extension with QFileDialog::getSaveFileName?
Andreas Pakulat
apaku at gmx.de
Wed Jun 22 19:41:22 CEST 2011
On 22.06.11 07:35:17, David Doria wrote:
> > Guys, nothing is wrong with the docu ;) The setDefaultSuffix method only has an impact on a QFileDialog *instance*, but the OP was calling the *static* method which triggers the creation of a *native* file dialog, which might or might not add the suffix by itself already. On Mac the extension is added depending on the current filter, if the filter happens to be unique (e.g. "PNG (*.png)" as opposed to "Images (*.png, *.jpg, *.bmp)"), on Windows not quite sure, KDE/Gnome native file dialogs apparently don't auto-suffix your filename.
> >
> > That said, I am not even sure whether setDefaultSuffix() has an impact on the *native* file dialog - might be that Qt expands the filename accordingly in "post production" (after the native dialog has returned).
> >
> > It works for sure with the Qt file dialog, but that one has several limitations and disadvantages. One being that the suffix cannot be auto-applied depending on the selected filter (if you want to save under multiple file formats, for instance, and the filter would specify the format such as "Template" or "Document"). Only one suffix can be supplied with setAutoSuffix.
> >
> > Cheers, Oliver
>
> Hi Oliver, they set me straight on making an instance of the dialog
> right away :)
>
> I'm not sure I follow the rest of what you said? Would you mind taking
> a look? The example has commented out an attempt with a static method
> (which apparently everyone agrees should not work), another commented
> section shows the attempt with setDefaultSuffix, and the uncommented
> section shows the manual method:
>
> http://programmingexamples.net/index.php?title=Qt/Widgets/FileDialogDefaultSuffix
First of all, that example is not minimal. You can but the code from
form.cpp into main.cpp directly. Anyway, your problem is that the second
commented part still calls the static function. If you look at the api
docs, there's only a single function called 'getSaveFileName' and thats
the static function. The fact that you use a QFileDialog instance to
call it does not change that inside the function there's no this pointer
and it is a static one.
To fix that, call dialog.open() or dialog.exec() and then when the
dialog has been closed via 'save' or 'ok' (or whatever label you've
used) fetch the files from it via selectedFiles().
Andreas
More information about the Qt-interest-old
mailing list