将一个Qlabel的QPixmap复制到另一个Qlabel

copy QPixmap of one Qlabel to another Qlabel

我正在尝试将 QPixmap 添加到从另一个 QLabel 获取的 QLabel,但是出现错误:

这是代码

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

如果我这样做:

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

要将数据从指针对象复制到对象,您必须使用 *

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

可以单行写成如下:

label2->setPixmap(*label1->pixmap());

请注意,* 会将 pixmap() 返回的指针转换为引用。 this thread.

中解释了两者之间的区别

请注意,在您的第一个示例中,第一行中构造的 QPixmap 从未被使用,并且发生了内存泄漏。第二行改变的是指针值,不是新建对象的数据。