QFileDialog::getSaveFileName and default selectedFilter

23,070

Solution 1

The problem partially solved, it seems to be a bug in my Qt version (4.7.4).

I wrote the following sample application:

#include <QApplication>
#include <QFileDialog>

int main(int argc, char **argv)
{
        QApplication app(argc,argv);

        QFileDialog::getSaveFileName(0,"Save file",QDir::currentPath(),
        "Music files (*.mp3);;Text files (*.txt);;All files (*.*)",
            new QString("Text files (*.txt)"));
        return 0;
}

and compiled it for 3 different platforms:

  1. Linux(OpenSUSE 12.1), Qt 4.7.4, gcc 4.6.2
  2. Linux(CentOS), Qt 4.7.3, gcc 4.1.2
  3. MS Windows, Qt 4.8.1, gcc 4.4.0

On the first platform the default filter in the dialog was "Music files", but on the second and the third it was "Text files", just as it meant to be.

Solution 2

You may try this sample application and verify, if it makes any difference. When you use direct dialog construction as in this case you have more control over the object.

#include <QApplication>
#include <QFileDialog>

int main(int argc, char **argv)
{
    QApplication app(argc,argv);

    QString filters("Music files (*.mp3);;Text files (*.txt);;All files (*.*)");
    QString defaultFilter("Text files (*.txt)");

    /* Static method approach */
    QFileDialog::getSaveFileName(0, "Save file", QDir::currentPath(),
        filters, &defaultFilter);

    /* Direct object construction approach */
    QFileDialog fileDialog(0, "Save file", QDir::currentPath(), filters);
    fileDialog.selectNameFilter(defaultFilter);
    fileDialog.exec();

    return 0;
}

Normally such kind of behavior is a sign of memory corruption. However, I've checked that with valgrind (I have Qt 4.8.1) and there are only some false positives from FontConfig.

Share:
23,070
Sergey
Author by

Sergey

Updated on July 22, 2022

Comments

  • Sergey
    Sergey almost 2 years

    I have getSaveFileName with some filters and I want one of them to be selected when user opens the "Save" dialog. Qt documentation says the following:

    The default filter can be chosen by setting selectedFilter to the desired value.

    I try the following variant:

    QString selFilter="All files (*.*)";
    QFileDialog::getSaveFileName(this,"Save file",QDir::currentPath(),
        "Text files (*.txt);;All files (*.*)",&selFilter);
    

    But when the dialog appears, the "Text files" filter (in general case, the first filter from the list) is selected. I also tried all of the following:

    selFilter="All files";
    selFilter="All files (*.*)\n";
    selFilter="All files (*.*);;";
    selFilter="All files (*.*)\0";
    

    and different mixtures of this variants. The format of the filter list in my code is done according to the documentation (example line from Qt docs):

    "Images (*.png *.xpm *.jpg);;Text files (*.txt);;XML files (*.xml)"
    

    Note that output to selFilter variable works properly: after user press OK, selFilter variable contains the filter selected by the user.

    Platform: Linux(OpenSUSE 12.1), Qt 4.7.4, gcc 4.6.2.

    So how to set up the default filter?!