Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct
Problem using setPixmap on a label
sorry for disturb.. I' m a very newbie with Qt and the C++ isn't my primary programming language..
I've a problem when i try to change the pixmap from a slot function:
Here the code :
Thanks a lot,
Hi and welcome to devnet,
Look your constructor again: you have shadowed your class member variable with local version. So when you call e.g. on_ResistenzeOn_clicked you are setting an uninitialized pixmap on your label.
Thanks for reply, i've looked my constructor but i cannot find the problem.. Sorry! Where have i shadowed my class member with local version? In the constructor i've initialized the QPixmap with relative image.
Thanks for patience.
From your constructor with comments:
ui->setupUi(this); QPixmap fire(":/images/Fire.png"); QPixmap fireScaled = fire.scaled(QSize(131,121),Qt::KeepAspectRatio); // << Shadowing your class member fireScaled QPixmap cold(":/images/cold.png"); QPixmap coldScaled = cold.scaled(QSize(131,121),Qt::KeepAspectRatio); // << Shadowing your class member coldeScaled
SGaist : Sorry i'm a noob...
Only the last question. I've solved the problem but i wish to know if this is trash code:
On mainwindow.h i've declared :
On mainwindow.c :
fire = new QPixmap(":/images/Fire.png");
fireScaled = fire->scaled(QSize(131,121),Qt::KeepAspectRatio);
cold = new QPixmap(":/images/cold.png");
coldScaled = cold->scaled(QSize(131,99),Qt::KeepAspectRatio);
Many thanks for your help and your patience.
First thing: you almost never need to allocated a QPixmap (or QImage for that matter) on the heap.
Second thing: since you only use fire and cold in the constructor it doesn't make sense to keep them.
Last thing: don't forget to handle properly the deletion of stuff you allocate on the heap.
Thanks a lot SGaist, problem solved!! :)