tags:

views:

319

answers:

1

I have a open file dialog with three filters:

QString fileName = QFileDialog::getOpenFileName(
        this,
        title,
        directory,
        tr("JPEG (*.jpg *.jpeg);; TIFF (*.tif);; All files (*.*)")
);

This displays a dialog with "JPEG" selected as the default filter. I wanted to put the filter list in alphabetical order so "All files" was first in the list. If I do this however, "All files" is the default selected filter - which I don't want.

Can I set the default selected filter for this dialog or do I have to go with the first specified filter?

I tried specifying a 5th argument (QString) to set the default selected filter but this didn't work. I think this might only be used to retrieve the filter that was set by the user.

+2  A: 

Like this:

QString selfilter = tr("JPEG (*.jpg *.jpeg)");
QString fileName = QFileDialog::getOpenFileName(
        this,
        title,
        directory,
        tr("All files (*.*);;JPEG (*.jpg *.jpeg);;TIFF (*.tif)" ),
        &selfilter 
);

I concur that the docs:
http://doc.trolltech.com/3.3/qfiledialog.html#getOpenFileName
are abit vague about this point but it only took me one try to guess how to do this right.

This is one case where it's faster to just try it than to ask a question.

shoosh
Thanks, I tried to inline the 5th arg
Greg K