PDA

View Full Version : QFileDialog::getSaveFileName default extension



elcuco
10th August 2007, 19:21
I want to add a extension to the file name a user chooses. Lets assume the piece of code attached. Lets also assume that the user chooses to save as "html", and he chooses the filename "file1".

I want to add to the filename, the "html" extension. However, on my code the variable sf contains the full string showed at the bottom 'HTML Files(*.html *.htm)' and I just want "html".

I can handcraft something myself, like having a hash, and creating the string to be passed to the QFileDialog::getSaveFileName call, but I am hoping for a better tirck.




static QString sf;
QString s = QFileDialog::getSaveFileName(
this,
"Choose a file to save to",
"",
"Text files (*.txt *.utf8);; HTML Files(*.html *.htm)",
&sf
);

if (s.isEmpty())
return;

bool status;
QString s_lower = s.toLower();

// what if the users did not specify a suffix...?
QFileInfo f( s );
if (f.suffix().isEmpty())
{
//
qDebug("no suffix, adding %s", qPrintable( sf ) );
}

marcel
10th August 2007, 19:26
Go ahead and use a map, or something like that.

Regards

elcuco
10th August 2007, 20:13
This is *so* lame... Trolltech needs to fix this issue, this is way wrong... anyway, this is what I am using right now (not very pleased with it, since the constructor now contains some text which should be part of the save function... which will make it hard to debug):



// somewhere in the constructor
extByMessage[ tr("Text files (*.txt *.utf8)") ] = ".txt";
extByMessage[ tr("HTML Files (*.html *.htm)") ] = ".html";

QHashIterator<QString, QString> i(extByMessage);
while (i.hasNext())
{
i.next();
saveMessage += i.key();
if (i.hasNext())
saveMessage += ";;";
}

// the save function
QString s = QFileDialog::getSaveFileName(
this,
tr("Choose a file to save to"),
lastDir,
saveMessage,
&sf
);

if (s.isEmpty())
return;

bool status;

// what if the users did not specify a suffix...?
QFileInfo f( s );
if (f.suffix().isEmpty())
{
// http://www.qtcentre.org/forum/f-qt-programming-2/t-qfiledialoggetsavefilename-default-extension-8503.html
s += extByMessage[sf];
}