View Full Version : problem with QFileDialog::getSaveFileName

25th June 2009, 11:01
Dear All,
I am using QFileDialog::getSaveFileName() with the option in following way (In Windows):
QFileDialog::getSaveFileName(this, tr("m_Save As"), QDir::currentPath(),
tr("%1 Files (*.%2)")
.arg( QString("BMP"))
.arg(QString("bmp")), 0, QFileDialog:: DontUseNativeDialog);

I am facing a problem with the return value. Whenever i specify a file name then returned filename will not have extensions. Ex: If i give "Image1" as file name, the above function will return the string Image1 along with the path instead of Image1.bmp.

If I dont use "QFileDialog:: DontUseNativeDialog" option then function will return properly e.i Image1.bmp. But I dont want to use native dialog.
Can anyone suggest how can resolve this problem.???

Thank you

26th June 2009, 06:03
please, anyone can help on this ???

26th June 2009, 06:43
Hi navi1084!

Yes, the native and non-native file dialogs behave differently, at least on windows. Note that the getSaveFileName function has a QString * selectedFilter parameter. In the selectedFilter you can select a default filter for the dialog, and after the function returns, the text will be changed to the selected filter. For example:

QString filter="XML files (*.xml)";
QString fileName=QFileDialog::getSaveFileName(this, tr("m_Save As"), QDir::currentPath(),
"Images (*.png *.xpm *.jpg);;Text files (*.txt);;XML files (*.xml)"
, &filter, QFileDialog:: DontUseNativeDialog);
In this case the xml filter will be selected by default, otherwise the default filter would be the first one in the list. Here comes the important part for us, after the function returns and in case you have selected the second filter the filter strings contents will be "Text files (*.txt)". By knowing the selected filter, you can decide which extension to append to the file name.

26th June 2009, 07:41

First take the user selected filter using selectedFilter parameter. Then, make your effort to give correct extension based on the selected filter.

However, the selectedFilter parameter doesn't work well under Linux until Qt 4.5.1. Such a problem does not occur under Windows nor under Mac OS.

26th June 2009, 08:38
Thanks for the reply and i agree what you are telling.
But when we use native dialog, the returned file name will be along with the extensiton. for example if user type name as Image1 and filter is *.BMP, then returned string will be image1.bmp. but is i am not using the native dialog then returned string is only Image1. Hence to append proper extension i need to do some work around (especially if user already types the extension).
Ultimately Qt's FileDialog doesn't act same as native dialog.