Partially revert "QLabel: cleanup QLabelPrivate"

This partially reverts commit 8bd532b1e.

Replacing the std::optional's with real objects led to unwanted
extra allocations, some even in the destructor.

Change-Id: Ib9f949b7d93352c9027ec8a162741d7830e9ec72
Reviewed-by: Volker Hilsheimer <volker.hilsheimer@qt.io>
(cherry picked from commit 6f62f25daffafab750f84aec2b90e129e14c2cd9)
Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
This commit is contained in:
hjk 2024-08-02 12:02:55 +02:00 committed by Qt Cherry-pick Bot
parent 70d46c20f1
commit e2233061d2
2 changed files with 33 additions and 28 deletions

View File

@ -138,7 +138,9 @@ QLabelPrivate::~QLabelPrivate()
QPicture QLabel::picture() const QPicture QLabel::picture() const
{ {
Q_D(const QLabel); Q_D(const QLabel);
return d->picture; if (d->picture)
return *(d->picture);
return QPicture();
} }
#endif // QT_NO_PICTURE #endif // QT_NO_PICTURE
@ -302,7 +304,7 @@ void QLabel::clear()
void QLabel::setPixmap(const QPixmap &pixmap) void QLabel::setPixmap(const QPixmap &pixmap)
{ {
Q_D(QLabel); Q_D(QLabel);
if (d->pixmap.cacheKey() != pixmap.cacheKey()) { if (!d->pixmap || d->pixmap->cacheKey() != pixmap.cacheKey()) {
d->clearContents(); d->clearContents();
d->pixmap = pixmap; d->pixmap = pixmap;
} }
@ -313,7 +315,9 @@ void QLabel::setPixmap(const QPixmap &pixmap)
QPixmap QLabel::pixmap() const QPixmap QLabel::pixmap() const
{ {
Q_D(const QLabel); 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; int vextra = hextra;
QFontMetrics fm = q->fontMetrics(); QFontMetrics fm = q->fontMetrics();
if (!pixmap.isNull()) { if (pixmap && !pixmap->isNull()) {
br = pixmap.rect(); br = pixmap->rect();
br.setSize(pixmap.deviceIndependentSize().toSize()); br.setSize(pixmap->deviceIndependentSize().toSize());
#ifndef QT_NO_PICTURE #ifndef QT_NO_PICTURE
} else if (!picture.isNull()) { } else if (picture && !picture->isNull()) {
br = picture.boundingRect(); br = picture->boundingRect();
#endif #endif
#if QT_CONFIG(movie) #if QT_CONFIG(movie)
} else if (movie && !movie->currentPixmap().isNull()) { } else if (movie && !movie->currentPixmap().isNull()) {
@ -1021,15 +1025,15 @@ void QLabel::paintEvent(QPaintEvent *)
} }
} else } else
#ifndef QT_NO_PICTURE #ifndef QT_NO_PICTURE
if (!d->picture.isNull()) { if (d->picture) {
QRect br = d->picture.boundingRect(); QRect br = d->picture->boundingRect();
int rw = br.width(); int rw = br.width();
int rh = br.height(); int rh = br.height();
if (d->scaledcontents) { if (d->scaledcontents) {
painter.save(); painter.save();
painter.translate(cr.x(), cr.y()); painter.translate(cr.x(), cr.y());
painter.scale((double)cr.width()/rw, (double)cr.height()/rh); 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(); painter.restore();
} else { } else {
int xo = 0; int xo = 0;
@ -1042,25 +1046,25 @@ void QLabel::paintEvent(QPaintEvent *)
xo = cr.width()-rw; xo = cr.width()-rw;
else if (align & Qt::AlignHCenter) else if (align & Qt::AlignHCenter)
xo = (cr.width()-rw)/2; 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 } else
#endif #endif
if (!d->pixmap.isNull()) { if (d->pixmap && !d->pixmap->isNull()) {
QPixmap pix; QPixmap pix;
const qreal dpr = devicePixelRatio(); 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) QSize scaledSize = d->scaledcontents ? (cr.size() * dpr)
: (d->pixmap.size() * (dpr / d->pixmap.devicePixelRatio())); : (d->pixmap->size() * (dpr / d->pixmap->devicePixelRatio()));
if (d->scaledpixmap.size() != scaledSize) { if (!d->scaledpixmap || d->scaledpixmap->size() != scaledSize) {
d->scaledpixmap = d->scaledpixmap =
d->pixmap.scaled(scaledSize, d->pixmap->scaled(scaledSize,
Qt::IgnoreAspectRatio, Qt::SmoothTransformation); Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
d->scaledpixmap.setDevicePixelRatio(dpr); d->scaledpixmap->setDevicePixelRatio(dpr);
} }
pix = d->scaledpixmap; pix = *d->scaledpixmap;
} else } else
pix = d->pixmap; pix = *d->pixmap;
QStyleOption opt; QStyleOption opt;
opt.initFrom(this); opt.initFrom(this);
if (!isEnabled()) if (!isEnabled())
@ -1260,10 +1264,10 @@ void QLabelPrivate::clearContents()
hasShortcut = false; hasShortcut = false;
#ifndef QT_NO_PICTURE #ifndef QT_NO_PICTURE
picture = QPicture(); picture.reset();
#endif #endif
scaledpixmap = QPixmap(); scaledpixmap.reset();
pixmap = QPixmap(); pixmap.reset();
text.clear(); text.clear();
Q_Q(QLabel); Q_Q(QLabel);
@ -1407,7 +1411,7 @@ void QLabel::setScaledContents(bool enable)
return; return;
d->scaledcontents = enable; d->scaledcontents = enable;
if (!enable) if (!enable)
d->scaledpixmap = QPixmap(); d->scaledpixmap.reset();
update(contentsRect()); update(contentsRect());
} }

View File

@ -35,6 +35,7 @@
#include <QtCore/qpointer.h> #include <QtCore/qpointer.h>
#include <array> #include <array>
#include <optional>
QT_BEGIN_NAMESPACE QT_BEGIN_NAMESPACE
@ -84,10 +85,10 @@ public:
mutable QSize sh; mutable QSize sh;
mutable QSize msh; mutable QSize msh;
QString text; QString text;
QPixmap pixmap; std::optional<QPixmap> pixmap;
QPixmap scaledpixmap; std::optional<QPixmap> scaledpixmap;
#ifndef QT_NO_PICTURE #ifndef QT_NO_PICTURE
QPicture picture; std::optional<QPicture> picture;
#endif #endif
#if QT_CONFIG(movie) #if QT_CONFIG(movie)
QPointer<QMovie> movie; QPointer<QMovie> movie;