Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Unsolved QLabel clearing pixmap when QFileDialog is selected.

    General and Desktop
    2
    2
    404
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • S
      Sriu1 last edited by Sriu1

      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 (
                  this,
                  "file",
                  "~/",
                  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

      jsulm 1 Reply Last reply Reply Quote 0
      • jsulm
        jsulm Lifetime Qt Champion @Sriu1 last edited by

        @Sriu1 Please read http://doc.qt.io/qt-5/qfiledialog.html#getOpenFileName
        "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);");
        

        https://forum.qt.io/topic/113070/qt-code-of-conduct

        1 Reply Last reply Reply Quote 2
        • First post
          Last post