Important: Please read the Qt Code of Conduct -

Move QPixmap to QLabel

  • Could I move QPixmap into the 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.

    If I haven't missed any fucntions Qt provided, what kind of
    technical reason QLabel do not support move semantics
    or just provide the users a simple function to send the resource into
    the QLabel but not copy.

    Thanks a lot

  • @ QLabel label;

  • Thanks, but this looks like copy
    According to the declaration
    setPixmap(QPixmap const &)
    Const& wouldn't increase reference count
    I don't think they will share the same resource

    and the implementation looks more like copy than move

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

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


    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.

    But the behavior I want is something like
    QPixmap *temp = origin_pixmap;
    origin_pixmap = new_pixmap;

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

Log in to reply