Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QLabel clearing pixmap when QFileDialog is selected.

QLabel clearing pixmap when QFileDialog is selected.

Scheduled Pinned Locked Moved Unsolved General and Desktop
2 Posts 2 Posters 710 Views
  • 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 Offline
    S Offline
    Sriu1
    wrote on last edited by Sriu1
    #1

    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

    jsulmJ 1 Reply Last reply
    0
    • S 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

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @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
      2

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved