简体   繁体   中英

QFileDialog does not give selected file

I have a little problem using the QFileDialog in Qt 5.2. When I open the dialog the dialog will show up but does not send me any selected files back. In the code example below the 'selectedFiles.at(0)' does not give anything. And the user selected the correct file. IS this a problem related to my code or to a bug in Qt 5.2?? Pls help.

QFileDialog dialog;
    QStringList selectedFiles;

    dialog.setFileMode(QFileDialog::AnyFile);
    dialog.setNameFilter("Images (*.png *.jpg)");

    if (dialog.exec())
    {
        selectedFiles = dialog.selectedFiles();

        QImage image(selectedFiles.at(0));

        if(image.height() != 320 && image.width() != 240)
        {
            QMessageBox messageBox;
            messageBox.setText("File is not an image or the dimension is not 320x240");
            messageBox.exec();
        }

        else
        {
            ui->browseLine->setText(selectedFiles.at(0));
        }
    }   

I don't see anything wrong in your code, I tested it and it worked just fine. What exactly do you mean by "'selectedFiles.at(0)' does not give anything"? Is selectedFiles empty (=contains no string) or does it return the empty string (=it contains the empty string)?

Anyhow; here is how I usually do this, maybe try the code and see if it works:

QStringList ls = QFileDialog::getOpenFileNames(this,
                                                   "Choose one or more files",
                                                   "",
                                                   "Audio-Files(*.wav)");
for(int i = 0; i < ls.size(); i++) {
    print(ls[i]);
}

i have the exact same issue with Qt 5.1.1 or Qt5.2RC1 on Mac Os X.9 ... the native standard open file stays on top (not always!, but 80% of the time), Hiding the app and showing it again make this dialog to disappear... Also sometime none of the files filtered will be shown.

Using non native Open Dialog works!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM