Problem using setPixmap on a label
-
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.
-
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 :
QPixmap *fire;
QPixmap fireScaled;
QPixmap *cold;
QPixmap coldScaled;On mainwindow.c :
ui->setupUi(this);
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);
QPixmap tempDown(":/images/tempdown.png");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.