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. label->setpixmap() problem with displaying
QtWS25 Last Chance

label->setpixmap() problem with displaying

Scheduled Pinned Locked Moved Solved General and Desktop
3 Posts 2 Posters 854 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.
  • F Offline
    F Offline
    fourfeeter
    wrote on last edited by
    #1

    Hello, I have simple code:

    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    
    MainWindow::MainWindow(QWidget *parent) :
        QMainWindow(parent),
        ui(new Ui::MainWindow)
    {
        ui->setupUi(this);
        przycisk1=0;
        przycisk2=0;
    
        QPixmap zielony("C:/Qt-projekty/arduinors232/zielony.png");
       QPixmap czerwony("C:/Qt-projekty/arduinors232/czerwony.png");
    
         ui->label_3->setPixmap(czerwony);
        ui->label_4->setPixmap(czerwony);
    }
    
    MainWindow::~MainWindow()
    {
        delete ui;
    }
    
    void MainWindow::on_pushButton_clicked()
    {
    
    
        if(przycisk1)
        {
            ui->label_3->setPixmap(czerwony);
     
            przycisk1=0;
            ui->pushButton->setText("Włącz");
    
        }
        else
        {
    
            ui->label_3->setPixmap(zielony);
               ui->pushButton->setText("Wyłącz");
            przycisk1=1;
    
        };
    
    
    
    }
    

    In constructor I give value of variable type QPixmap. In next I am using function setPixmap(), I display picture. It working good. The problem is in function on_pushButton_clicked(). A can't display the picture. What is interesting, when I whill modify this code and Qpixmap give value of variable in function on_pushButton_clicked(), everything is working? Where is problem?

    PS.
    Sorry of my engilsh.

    mrjjM 1 Reply Last reply
    0
    • F fourfeeter

      Hello, I have simple code:

      #include "mainwindow.h"
      #include "ui_mainwindow.h"
      
      MainWindow::MainWindow(QWidget *parent) :
          QMainWindow(parent),
          ui(new Ui::MainWindow)
      {
          ui->setupUi(this);
          przycisk1=0;
          przycisk2=0;
      
          QPixmap zielony("C:/Qt-projekty/arduinors232/zielony.png");
         QPixmap czerwony("C:/Qt-projekty/arduinors232/czerwony.png");
      
           ui->label_3->setPixmap(czerwony);
          ui->label_4->setPixmap(czerwony);
      }
      
      MainWindow::~MainWindow()
      {
          delete ui;
      }
      
      void MainWindow::on_pushButton_clicked()
      {
      
      
          if(przycisk1)
          {
              ui->label_3->setPixmap(czerwony);
       
              przycisk1=0;
              ui->pushButton->setText("Włącz");
      
          }
          else
          {
      
              ui->label_3->setPixmap(zielony);
                 ui->pushButton->setText("Wyłącz");
              przycisk1=1;
      
          };
      
      
      
      }
      

      In constructor I give value of variable type QPixmap. In next I am using function setPixmap(), I display picture. It working good. The problem is in function on_pushButton_clicked(). A can't display the picture. What is interesting, when I whill modify this code and Qpixmap give value of variable in function on_pushButton_clicked(), everything is working? Where is problem?

      PS.
      Sorry of my engilsh.

      mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @fourfeeter
      Hi
      In constructor, you have the pixmaps as local variables
      QPixmap zielony("C:/Qt-projekty/arduinors232/zielony.png"); <<< local . Cannot be seen outside constructor
      QPixmap czerwony("C:/Qt-projekty/arduinors232/czerwony.png"); <<< local

      so I guess in
      void MainWindow::on_pushButton_clicked() {
      ui->label_3->setPixmap(czerwony); <<<< that is other one. Not the one from constructor ?

      So I guess you mean
      zielony.load("C:/Qt-projekty/arduinors232/zielony.png");
      czerwony.load("C:/Qt-projekty/arduinors232/czerwony.png");

      if zielony and czerwony are defined in .h ( the class)

      1 Reply Last reply
      2
      • F Offline
        F Offline
        fourfeeter
        wrote on last edited by
        #3

        OK. This was a problem. Now it works. Thanks for the help.

        1 Reply Last reply
        1

        • Login

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