Skip to content

Commit c63c918

Browse files
chehrlicQt Cherry-pick Bot
authored and
Qt Cherry-pick Bot
committed
QLabel: don't cache scaled pixmap in QIcon
Caching the scaled QPixmap directly in QIcon is not a good idea - the cached pixmap might be used as starting point for a pixmap with another size so it gets blurry - QIcon has no caching mechanism to throw away unneeded QPixmaps after some time so the memory usage grows indefinitely Fixes: QTBUG-134930 Change-Id: Ic490ba15438a5cd07a555692e1d08cd1c211d005 Reviewed-by: Volker Hilsheimer <[email protected]> (cherry picked from commit 7a238e1) Reviewed-by: Qt Cherry-pick Bot <[email protected]> (cherry picked from commit 04cb0a1) Reviewed-by: Topi Reiniö <[email protected]>
1 parent 4c02701 commit c63c918

File tree

1 file changed

+16
-3
lines changed

1 file changed

+16
-3
lines changed

src/widgets/widgets/qlabel.cpp

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include "qstyle.h"
77
#include "qstyleoption.h"
88
#include "qlabel_p.h"
9+
#include "private/qhexstring_p.h"
910
#include "private/qstylesheetstyle_p.h"
1011
#include <qmath.h>
1112

@@ -1054,9 +1055,21 @@ void QLabel::paintEvent(QPaintEvent *)
10541055
const auto mode = isEnabled() ? QIcon::Normal : QIcon::Disabled;
10551056
QPixmap pix = d->icon->pixmap(size, dpr, mode);
10561057
if (d->scaledcontents && pix.size() != size * dpr) {
1057-
pix = pix.scaled(size * dpr, Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
1058-
pix.setDevicePixelRatio(dpr);
1059-
d->icon->addPixmap(pix, mode);
1058+
const QString key = "qt_label_"_L1 % HexString<quint64>(pix.cacheKey())
1059+
% HexString<quint8>(mode)
1060+
% HexString<uint>(size.width())
1061+
% HexString<uint>(size.height())
1062+
% HexString<quint16>(qRound(dpr * 1000));
1063+
if (!QPixmapCache::find(key, &pix)) {
1064+
pix = pix.scaled(size * dpr, Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
1065+
pix.setDevicePixelRatio(dpr);
1066+
// using QIcon to cache the newly create pixmap is not possible
1067+
// because QIcon does not clear this cache (so we grow indefinitely)
1068+
// and also uses the newly added pixmap as starting point for new
1069+
// scaled pixmap which makes it very blurry.
1070+
// Therefore use QPixmapCache here.
1071+
QPixmapCache::insert(key, pix);
1072+
}
10601073
}
10611074
QStyleOption opt;
10621075
opt.initFrom(this);

0 commit comments

Comments
 (0)