简体   繁体   English

QFileDialog不提供所选文件

[英]QFileDialog does not give selected file

I have a little problem using the QFileDialog in Qt 5.2. 我在Qt 5.2中使用QFileDialog时遇到了一些问题。 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. 在下面的代码示例中,'selectedFiles.at(0)'没有提供任何内容。 And the user selected the correct file. 并且用户选择了正确的文件。 IS this a problem related to my code or to a bug in Qt 5.2?? 这是一个与我的代码或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"? 究竟是什么意思“'selectedFiles.at(0)'没有给出什么”? Is selectedFiles empty (=contains no string) or does it return the empty string (=it contains the empty string)? selectedFiles是空的(=不包含字符串)还是返回空字符串(=它包含空字符串)?

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. 我在Mac OS X.9上与Qt 5.1.1或Qt5.2RC1完全相同的问题...本机标准打开文件保持在最顶层(并非总是!,但80%的时间),隐藏应用程序并显示它再次使这个对话框消失...有时候,所有过滤的文件都不会显示。

Using non native Open Dialog works! 使用非原生Open Dialog有效!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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