label->setpixmap() problem with displaying
-
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. -
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.@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"); <<< localso 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)
-
OK. This was a problem. Now it works. Thanks for the help.