Important: Please read the Qt Code of Conduct -

Problem using setPixmap on a label

  • Hi all,
    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,

  • Lifetime Qt Champion

    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.

  • Hi,
    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.

  • Lifetime Qt Champion

    From your constructor with comments:

    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 :
    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.

  • Lifetime Qt Champion

    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!! :)

Log in to reply