Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

QFileDialog::getSaveFileName and default selectedFilter

Tags:

c++

linux

qt

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?!

like image 754
Sergey Avatar asked Sep 22 '12 17:09

Sergey


1 Answers

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.

like image 57
divanov Avatar answered Oct 30 '22 16:10

divanov