简体   繁体   English

将 QPixmap 移动到 QLabel 但不复制

[英]move QPixmap to QLabel but not copy

Could I move QPixmap into the QLabel?我可以将 QPixmap 移动到 QLabel 中吗? I haven't found any member function which support move semantics or some function could move the QPixmap into the QLabel even in Qt5.The original solution of QLabel is copy the QPixmap you pass into the function.我还没有找到任何支持移动语义的成员函数,或者即使在 Qt5 中,某些函数也可以将 QPixmap 移动到 QLabel 中。 QLabel 的原始解决方案是将您传递给函数的 QPixmap 复制。

According to the declaration根据声明

setPixmap(QPixmap const &)

Const& wouldn't increase reference count(any mistake?) I don't think they will share the same resource Const& 不会增加引用计数(有什么错误吗?)我认为他们不会共享相同的资源

and the implementation looks more like copy than move(Qt4.8.3)并且实现看起来更像是复制而不是移动(Qt4.8.3)

void QLabel::setPixmap(const QPixmap &pixmap)
{
    Q_D(QLabel);
    if (!d->pixmap || d->pixmap->cacheKey() != pixmap.cacheKey()) {
        d->clearContents();
        d->pixmap = new QPixmap(pixmap);
    }

    if (d->pixmap->depth() == 1 && !d->pixmap->mask())
        d->pixmap->setMask(*((QBitmap *)d->pixmap));

    d->updateLabel();
}

looks like the QLabel will create a brand new copy of the QPixmap if the d->pixmap do not point to anything or they don't have the same cacheKey.如果 d->pixmap 不指向任何东西或者它们没有相同的缓存键,看起来 QLabel 将创建 QPixmap 的全新副本。

But the behavior I want is something like但我想要的行为就像

QPixmap *temp = origin_pixmap;
origin_pixmap = new_pixmap;
temp->release()

or或者

std::string A, B;
//.............
A = std::move(B);

Pixmap 实现了Copy On Write概念,所以复制一个像素图并不是一个繁重的操作,只有当你尝试修改原始像素图时才会发生实际的数据复制。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM