PDA

View Full Version : Differences in QFileDialog::getSaveFileName between Windows and Linux



negritot
22nd July 2009, 18:01
I'm seeing an aggravating difference in the behavior of QFileDialog::getSaveFileName on Windows vs. Linux. The difference is and whether or not the correct file name extension is automatically appended to the filename that the user enters. For example:


QString fileName = QFileDialog::getSaveFileName(parent,
"Select save location",
QString(),
"Text Files (*.txt)");

On Windows, if the user enters "temp" as the filename and clicks okay, the variable "fileName" will contain the string "temp.txt". On Linux, it will only contain the string "temp".

I would much prefer the behavior that I see on Windows, so that I don't have to implement this behavior myself every time I use this method. Does anyone have any ideas about how to get the correct behavior on Linux, or is this just a bug have to deal with for the time being?

I'm using Qt 4.5.1.

mcosta
22nd July 2009, 21:00
As default in MS Windows QFileDialog::getSaveFileName uses native Dialogs

wysota
22nd July 2009, 21:16
I would much prefer the behavior that I see on Windows, so that I don't have to implement this behavior myself every time I use this method.

It's enough if you implement it once and reuse the code afterwards...


QString getSaveFileNameWithExtension(QWidget * parent = 0, const QString & caption = QString(),
const QString & dir = QString(), const QString & filter = QString(),
QString * selectedFilter = 0, Options options = 0){
QString res = QFileDialog::getSaveFileName(parent, caption, dir, filter, selectedFilter, options);
#ifndef Q_OS_WIN
if(selectedFilter){
QRegExp filterRx(".*\\(\\*\\.(.*)\\)");
if(filterRx.exactMatch(*selectedFilter))
res.append("."+filterRx.cap(1));
}
#endif
return res;
}
Note this of course won't work if you select a filter such as "Images (*.png *.xpm *.jpg)" but I understand it wouldn't work on Windows either.