Important: Please read the Qt Code of Conduct -

QLabel clearing pixmap when QFileDialog is selected.

  • I have a label and a pushButton which sets image to the label on clicked.If label initially has a image set and If the button is clicked the label gets cleared even before selecting image from the FileDialog.

    Below is the code

    void MainWindow::on_backgroundImageBrowse_clicked()

        qstrFileName = QFileDialog::getOpenFileName (
                tr ("Image (*.bmp *.jpg );;All files (*.*)")
        m_ZoneManager->GUI_generalstruct->generalBackgroundImage = qstrFileName;
        QPixmap pix (m_ZoneManager->GUI_generalstruct->generalBackgroundImage);
        ui->label->setStyleSheet ("border-image:url(m_ZoneManager->GUI_generalstruct->generalBackgroundImage);");
        int w = 1280;
        int h = 720;
        ui->label->setPixmap (pix.scaled (w, h)); //,Qt::KeepAspectRatio


    Even If cancel is clicked in the fileDialog the label is getting cleared.Please suggest

  • Lifetime Qt Champion

    @Sriu1 Please read
    "If the user presses Cancel, it returns a null string."
    I don't see you checking whether qstrFileName is a null string or not.
    Also this line does not make any sence:

    ui->label->setStyleSheet ("border-image:url(m_ZoneManager->GUI_generalstruct->generalBackgroundImage);");

Log in to reply