Search code examples
c++qtqt5qlabelqpixmap

copy QPixmap of one Qlabel to another Qlabel


I am trying to add a QPixmap to a QLabel taken from another QLabel but there is an error :

Here is the code

const QPixmap *tempPix = new QPixmap("");
tempPix = (label1->pixmap());
label2->setPixmap(tempPix);  //error cannot convert from const QPixmap* to const QPixmap&

and if I do it like this:

const QPixmap tempPix("");
tempPix = (label1->pixmap()); //error cannot convert QPixmap and QPixmap*
label2->setPixmap(tempPix);

Solution

  • To copy data from a pointer object to an object you must use the *

    QPixmap tempPix;
    if(label1->pixmap()){
        tempPix = *label1->pixmap();
        label2->setPixmap(tempPix);
     }