From 9c947c131d05d075f29fcfef6b0f36e38b30567b Mon Sep 17 00:00:00 2001 From: Giuseppe D'Angelo Date: Wed, 23 Feb 2022 17:06:49 +0100 Subject: [PATCH] QLabel: remove some dynamic allocations QLabelPrivate uses pointers and dynamically allocated objects as a form of optional. To add insult to injury, the objects are implictly shared, compounding the effects in terms of required allocations. Just use std::optional instead. Change-Id: Ica582dc0d2a9ab56f45ce305290541383b7ddeef Reviewed-by: Richard Moe Gustavsen --- src/widgets/widgets/qlabel.cpp | 36 ++++++++++++++-------------------- src/widgets/widgets/qlabel_p.h | 10 ++++++---- 2 files changed, 21 insertions(+), 25 deletions(-) diff --git a/src/widgets/widgets/qlabel.cpp b/src/widgets/widgets/qlabel.cpp index 997df9d992..864b2a57c8 100644 --- a/src/widgets/widgets/qlabel.cpp +++ b/src/widgets/widgets/qlabel.cpp @@ -65,11 +65,11 @@ QLabelPrivate::QLabelPrivate() sh(), msh(), text(), - pixmap(nullptr), - scaledpixmap(nullptr), - cachedimage(nullptr), + pixmap(), + scaledpixmap(), + cachedimage(), #ifndef QT_NO_PICTURE - picture(nullptr), + picture(), #endif #if QT_CONFIG(movie) movie(), @@ -375,7 +375,7 @@ void QLabel::setPixmap(const QPixmap &pixmap) Q_D(QLabel); if (!d->pixmap || d->pixmap->cacheKey() != pixmap.cacheKey()) { d->clearContents(); - d->pixmap = new QPixmap(pixmap); + d->pixmap = pixmap; } d->updateLabel(); @@ -420,7 +420,7 @@ void QLabel::setPicture(const QPicture &picture) { Q_D(QLabel); d->clearContents(); - d->picture = new QPicture(picture); + d->picture = picture; d->updateLabel(); } @@ -1129,12 +1129,12 @@ void QLabel::paintEvent(QPaintEvent *) QSize scaledSize = cr.size() * devicePixelRatio(); if (!d->scaledpixmap || d->scaledpixmap->size() != scaledSize) { if (!d->cachedimage) - d->cachedimage = new QImage(d->pixmap->toImage()); - delete d->scaledpixmap; + d->cachedimage = d->pixmap->toImage(); + d->scaledpixmap.reset(); QImage scaledImage = d->cachedimage->scaled(scaledSize, Qt::IgnoreAspectRatio, Qt::SmoothTransformation); - d->scaledpixmap = new QPixmap(QPixmap::fromImage(std::move(scaledImage))); + d->scaledpixmap = QPixmap::fromImage(std::move(scaledImage)); d->scaledpixmap->setDevicePixelRatio(devicePixelRatio()); } pix = *d->scaledpixmap; @@ -1335,15 +1335,11 @@ void QLabelPrivate::clearContents() hasShortcut = false; #ifndef QT_NO_PICTURE - delete picture; - picture = nullptr; + picture.reset(); #endif - delete scaledpixmap; - scaledpixmap = nullptr; - delete cachedimage; - cachedimage = nullptr; - delete pixmap; - pixmap = nullptr; + scaledpixmap.reset(); + cachedimage.reset(); + pixmap.reset(); text.clear(); Q_Q(QLabel); @@ -1489,10 +1485,8 @@ void QLabel::setScaledContents(bool enable) return; d->scaledcontents = enable; if (!enable) { - delete d->scaledpixmap; - d->scaledpixmap = nullptr; - delete d->cachedimage; - d->cachedimage = nullptr; + d->scaledpixmap.reset(); + d->cachedimage.reset(); } update(contentsRect()); } diff --git a/src/widgets/widgets/qlabel_p.h b/src/widgets/widgets/qlabel_p.h index d26bbf6a7f..b7e347b1d5 100644 --- a/src/widgets/widgets/qlabel_p.h +++ b/src/widgets/widgets/qlabel_p.h @@ -69,6 +69,8 @@ #include "qmenu.h" #endif +#include + QT_BEGIN_NAMESPACE class Q_AUTOTEST_EXPORT QLabelPrivate : public QFramePrivate @@ -117,11 +119,11 @@ public: mutable QSize sh; mutable QSize msh; QString text; - QPixmap *pixmap; - QPixmap *scaledpixmap; - QImage *cachedimage; + std::optional pixmap; + std::optional scaledpixmap; + std::optional cachedimage; #ifndef QT_NO_PICTURE - QPicture *picture; + std::optional picture; #endif #if QT_CONFIG(movie) QPointer movie;