Giova84
Giova84

Reputation: 45

QFileDialog: - set the extension to the saved file according to the user choice

I have the following code (of a rich text editor written in QT 4.8) on which I'm working on:

bool TextEdit::fileSaveAs()
{
    QString fn = QFileDialog::getSaveFileName(this, tr("Save as..."),
                                              QString(), tr("ODT document (*.odt);;HTML-Files (*.htm *.html)"), 0, QFileDialog::DontUseNativeDialog );

    if (fn.isEmpty())
        return false;
    if (! fn.endsWith(".txt", Qt::CaseInsensitive) || (fn.endsWith(".odt", Qt::CaseInsensitive) || fn.endsWith(".htm", Qt::CaseInsensitive) || fn.endsWith(".html", Qt::CaseInsensitive)) )
        fn += ".odt"; // default
    setCurrentFileName(fn);
    return fileSave();
}

The save dialog window allows to choose between *.odt and *.html extensions; however, by default, is always set the *.odt extension (see fn += ".odt"). I know that I can change this one to html, but I aim to get rid of the forced extension set inside the code and let the document to be saved with the extension selected in the save dialog window:


(source: funkyimg.com)

How can I accomplish this? Can someone suggest me some practical example, considering that I am a newbie about coding?

Upvotes: 0

Views: 1230

Answers (1)

me_alok
me_alok

Reputation: 251

Use another constructor with selectedfilter argument, the result would be

QString selectedFilter;
QString fn = QFileDialog::getSaveFileName 
    (this,
    tr("Save as..."),
    QString(),
    tr("ODT document (*.odt);;HTML-Files (*.htm *.html)"),
    0,
    QFileDialog::DontUseNativeDialog,
    &selectedFilter);

Upvotes: 1

Related Questions