[Qt-interest] Force file extension with QFileDialog::getSaveFileName?
Constantin Makshin
cmakshin at gmail.com
Wed Jun 22 19:45:03 CEST 2011
I had read the thread before posting my message.
Initially David said he tried this:
QFileDialog dialog(this);
dialog.setFilter(".png .bmp");
dialog.setDefaultSuffix(".png");
cout << dialog.getSaveFileName().toStdString();
and had no luck. Then (exactly in the message I answered to) he provided a link to the page with this:
// For some reason this doesn't work?
QFileDialog dialog(this);
//dialog.setFilter(".png .bmp");
dialog.setNameFilter("*.png;;*.bmp");
dialog.setDefaultSuffix("png");
QString fileName = dialog.getSaveFileName();
Yes, in both cases he used the setDefaultSuffix() function. But then he called getSaveFileName() and was (or, at least, it seemed like he was) surprised that didn't work. But we all know getSaveFileName() will remain static (and thus unaware of any customizations made by setDefaultSuffix() and other non-static functions) no matter how one calls it — as QFileDialog::getSaveFileName() or as dialog.getSaveFileName().
And again, I apologize if any of my messages insulted anyone.
On Wednesday, June 22, 2011 09:13:19 PM Eric Clark wrote:
>
> > -----Original Message-----
> > From: qt-interest-bounces+eclark=ara.com at qt.nokia.com [mailto:qt-
> > interest-bounces+eclark=ara.com at qt.nokia.com] On Behalf Of Constantin
> > Makshin
> > Sent: Wednesday, June 22, 2011 12:03 PM
> > To: Qt Interest
> > Subject: Re: [Qt-interest] Force file extension with
> > QFileDialog::getSaveFileName?
> >
> > I don't want to insult anyone, but there's a problem with the way those who
> > expect setDefaultSuffix() to affect getSaveFileName() think. There's simply
> > no logic in that — getSaveFileName() is a *static* function, so there's no way
> > it can access non-static object members (just because static functions don't
> > have any object to work with) set by setDefaultSuffix(). It's not specific to Qt
> > or even C++, it's the way static functions work in any programming language.
> > I apologize if that sounds too rude.
>
> It is only rude because you did not pay attention to everything that was said in the thread. Both David and I are fully aware of the fact that the function is only to be used with an instance of the dialog and not the static functions. Please pay attention to the whole thread next time before you go trying to explain how we don’t understand static functions in any language.
>
> David made an attempt to use the setDefaultSuffix function with an instance of the QFileDialog class, not the static function getSaveFileName, and it still does not do what the documentation says it should. So, if you want to help please don’t just show up in the middle of it without knowledge of everything that was going on. Or at least read the whole thread before you respond.
>
> Thanks,
> Eric
>
> >
> > As for the question about default extension in getSaveFileName(), in my
> > application I call it this way:
> >
> > QString fileName = QFileDialog::getSaveFileName(this, tr("Save Technique"),
> > m_technique->fileName(), MainWindow::tr("Production Techniques XML
> > (*.ptx *.ptxml);;All Files (*.*)"));
> >
> > And if the user doesn't change the filter and doesn't explicitly specify any file
> > extension, "ptx" is added automatically. I.e. if the user types "somefile" into
> > the file name box, fileName will be equal to "somefile.ptx". And this works
> > correctly at least on Windows and GNU/Linux with KDE (once I asked a friend
> > to test the application on Mac OS X and didn't get any complaints related to
> > default extension, but there was no thorough testing on that platform).
> >
> > On Wednesday, June 22, 2011 03:35:17 PM 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/FileDialogDe
> > > faultSuffix
> > >
> > > Again the documentation says "This property specifies a string that
> > > will be added to the filename if it has no suffix already." and I'm
> > > just not seeing that happen.
> > >
> > > Thanks,
> > >
> > > David
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 490 bytes
Desc: This is a digitally signed message part.
Url : http://lists.qt-project.org/pipermail/qt-interest-old/attachments/20110622/82753426/attachment.bin
More information about the Qt-interest-old
mailing list