diff --git a/src/widgets/widgets/qlabel.cpp b/src/widgets/widgets/qlabel.cpp index a5681c0857e..5694a485917 100644 --- a/src/widgets/widgets/qlabel.cpp +++ b/src/widgets/widgets/qlabel.cpp @@ -138,7 +138,9 @@ QLabelPrivate::~QLabelPrivate() QPicture QLabel::picture() const { Q_D(const QLabel); - return d->picture; + if (d->picture) + return *(d->picture); + return QPicture(); } #endif // QT_NO_PICTURE @@ -302,7 +304,7 @@ void QLabel::clear() void QLabel::setPixmap(const QPixmap &pixmap) { Q_D(QLabel); - if (d->pixmap.cacheKey() != pixmap.cacheKey()) { + if (!d->pixmap || d->pixmap->cacheKey() != pixmap.cacheKey()) { d->clearContents(); d->pixmap = pixmap; } @@ -313,7 +315,9 @@ void QLabel::setPixmap(const QPixmap &pixmap) QPixmap QLabel::pixmap() const { Q_D(const QLabel); - return d->pixmap; + if (d->pixmap) + return *(d->pixmap); + return QPixmap(); } /*! @@ -521,12 +525,12 @@ QSize QLabelPrivate::sizeForWidth(int w) const int vextra = hextra; QFontMetrics fm = q->fontMetrics(); - if (!pixmap.isNull()) { - br = pixmap.rect(); - br.setSize(pixmap.deviceIndependentSize().toSize()); + if (pixmap && !pixmap->isNull()) { + br = pixmap->rect(); + br.setSize(pixmap->deviceIndependentSize().toSize()); #ifndef QT_NO_PICTURE - } else if (!picture.isNull()) { - br = picture.boundingRect(); + } else if (picture && !picture->isNull()) { + br = picture->boundingRect(); #endif #if QT_CONFIG(movie) } else if (movie && !movie->currentPixmap().isNull()) { @@ -1021,15 +1025,15 @@ void QLabel::paintEvent(QPaintEvent *) } } else #ifndef QT_NO_PICTURE - if (!d->picture.isNull()) { - QRect br = d->picture.boundingRect(); + if (d->picture) { + QRect br = d->picture->boundingRect(); int rw = br.width(); int rh = br.height(); if (d->scaledcontents) { painter.save(); painter.translate(cr.x(), cr.y()); painter.scale((double)cr.width()/rw, (double)cr.height()/rh); - painter.drawPicture(-br.x(), -br.y(), d->picture); + painter.drawPicture(-br.x(), -br.y(), *d->picture); painter.restore(); } else { int xo = 0; @@ -1042,25 +1046,25 @@ void QLabel::paintEvent(QPaintEvent *) xo = cr.width()-rw; else if (align & Qt::AlignHCenter) xo = (cr.width()-rw)/2; - painter.drawPicture(cr.x()+xo-br.x(), cr.y()+yo-br.y(), d->picture); + painter.drawPicture(cr.x()+xo-br.x(), cr.y()+yo-br.y(), *d->picture); } } else #endif - if (!d->pixmap.isNull()) { + if (d->pixmap && !d->pixmap->isNull()) { QPixmap pix; const qreal dpr = devicePixelRatio(); - if (d->scaledcontents || dpr != d->pixmap.devicePixelRatio()) { + if (d->scaledcontents || dpr != d->pixmap->devicePixelRatio()) { QSize scaledSize = d->scaledcontents ? (cr.size() * dpr) - : (d->pixmap.size() * (dpr / d->pixmap.devicePixelRatio())); - if (d->scaledpixmap.size() != scaledSize) { + : (d->pixmap->size() * (dpr / d->pixmap->devicePixelRatio())); + if (!d->scaledpixmap || d->scaledpixmap->size() != scaledSize) { d->scaledpixmap = - d->pixmap.scaled(scaledSize, - Qt::IgnoreAspectRatio, Qt::SmoothTransformation); - d->scaledpixmap.setDevicePixelRatio(dpr); + d->pixmap->scaled(scaledSize, + Qt::IgnoreAspectRatio, Qt::SmoothTransformation); + d->scaledpixmap->setDevicePixelRatio(dpr); } - pix = d->scaledpixmap; + pix = *d->scaledpixmap; } else - pix = d->pixmap; + pix = *d->pixmap; QStyleOption opt; opt.initFrom(this); if (!isEnabled()) @@ -1260,10 +1264,10 @@ void QLabelPrivate::clearContents() hasShortcut = false; #ifndef QT_NO_PICTURE - picture = QPicture(); + picture.reset(); #endif - scaledpixmap = QPixmap(); - pixmap = QPixmap(); + scaledpixmap.reset(); + pixmap.reset(); text.clear(); Q_Q(QLabel); @@ -1407,7 +1411,7 @@ void QLabel::setScaledContents(bool enable) return; d->scaledcontents = enable; if (!enable) - d->scaledpixmap = QPixmap(); + d->scaledpixmap.reset(); update(contentsRect()); } diff --git a/src/widgets/widgets/qlabel_p.h b/src/widgets/widgets/qlabel_p.h index d6127f1e544..6d3b46f29ae 100644 --- a/src/widgets/widgets/qlabel_p.h +++ b/src/widgets/widgets/qlabel_p.h @@ -35,6 +35,7 @@ #include #include +#include QT_BEGIN_NAMESPACE @@ -84,10 +85,10 @@ public: mutable QSize sh; mutable QSize msh; QString text; - QPixmap pixmap; - QPixmap scaledpixmap; + std::optional pixmap; + std::optional scaledpixmap; #ifndef QT_NO_PICTURE - QPicture picture; + std::optional picture; #endif #if QT_CONFIG(movie) QPointer movie;