7

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

Sergey
  • 7,985
  • 4
  • 48
  • 80

2 Answers2

1

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.

divanov
  • 6,173
  • 3
  • 32
  • 51
  • I tried your sample application, result is the same: it works fine with Qt 4.7.3 and 4.8.1, but not with 4.7.4. Moreover, with Qt 4.7.4 (not other versions), direct object construction produces following warnings: KGlobal::locale::Warning your global KLocale is being recreated with a valid main component instead of a fake component, this usually means you tried to call i18n related functions before your main component was created. You should not do that since it most likely will not work kfilemodule(13490) KSambaSharePrivate::findSmbConf: KSambaShare: Could not find smb.conf! – Sergey Sep 23 '12 at 10:07
  • This is something related to KDE setup as should have no effect to such programs. What if you install valgrind and try memory profiling with a command: valgrind --tool=memcheck --leak-check=yes --show-reachable=yes --num-callers=20 --track-fds=yes ./your_binary &> mem.log Then you can check mem.log for presence of "Invalid read" and "Invalid write". – divanov Sep 23 '12 at 12:20
  • I tried valgrind: it shows invalid reads and writes in libfontconfig.so (as you wrote above), 7 open sockets left on exit, 1 conditional jump in libQtGui, that depends on uninitialized variable, and lots of leaks in libQtCore and other third-party libraries. But it's normal, valgrind gives such output for any other Qt program. – Sergey Sep 23 '12 at 13:25
0

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.

Sergey
  • 7,985
  • 4
  • 48
  • 80
  • 3
    Using operator new in method parameters results in a memory leak as QFileDialog does not own and free defaultFilter parameter. – divanov Sep 23 '12 at 09:02