PDA

View Full Version : QFileDialog selecting file or directory



^NyAw^
19th June 2008, 15:29
Hi,

I want a QFileDialog that could select "mp3" extension files and also a directory could be selected(note that I only want one selection, so you can select one MP3 file or a directory).
I've inserted "Music (*.mp3)" as filter but don't know how to let it select dirs.

Thanks,

^NyAw^
19th June 2008, 15:51
Hi,

Have solved it just calling "setFileMode(QFileDialog::Directory)" to the dialog.
The problem is that now the dialog is different that if I use the static function "QFileDialog::getOpenFileName". It is not look native and it is not translated.

Thanks,

Fatla
19th June 2008, 16:01
I hope that 'll be useful .
That's Code I use to filter JPG & set the path @ a Label :




QFileDialog::Options options;

options |= QFileDialog::DontUseNativeDialog;
QString selectedFilter;

QString fileName = QFileDialog::getOpenFileName(this,
tr("Open"),
formTwo.lblSelectedFilePath->text(),
tr("JPEG (*.JPG)"),
&selectedFilter,
options);


if (!fileName.isEmpty())
formTwo.lblSelectedFilePath->setText(fileName);

^NyAw^
20th June 2008, 09:28
Hi,

I'm having the problem yet. The dialog only let me selec directories now.

Thanks,

DougRoyer
13th November 2021, 20:57
This fails for me. I can only select files. No way to select the directory without opening it up. The question was how to select a file, or a directory. This only selects files.

d_stranz
13th November 2021, 23:03
If you are using the QFileDialog static methods, then you call QFileDialog::getExistingDirectory() to select directories only and QFileDialog::getOpenFileName() for files only. If you are using the non-static method (ie. via QFileDialog::exec()) than you set the option QFileDialog::ShowDirsOnly and use QFileDialog::Directory file mode.

The original post is 13 years old, during Qt 3 or Qt 4 days. Qt is three major revisions past that now.