From 6790d8d716d330993d892b5df14b3e3cb6b8343a Mon Sep 17 00:00:00 2001 From: Christian Ehrlicher Date: Tue, 24 Dec 2024 14:16:00 +0100 Subject: [PATCH] QCommandLinkButton: fix drawing high-dpi icons QCommandLinkButton was using QIcon::pixmap() without specifying the current device pixel ratio. Switch to QIcon::paint() to not have to fiddle around with the dpr at all here. As a drive-by remove a useless QPainter::save/restore call. Pick-to: 6.8 Change-Id: I7e2492a09b28cb8a4f4cc60454733e0054fe1e9b Reviewed-by: Volker Hilsheimer (cherry picked from commit 785fb89e0aafc25a435d28ff05bf0dc05385b372) Reviewed-by: Qt Cherry-pick Bot --- src/widgets/widgets/qcommandlinkbutton.cpp | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/src/widgets/widgets/qcommandlinkbutton.cpp b/src/widgets/widgets/qcommandlinkbutton.cpp index d8fa2daf7c8..815e00e5498 100644 --- a/src/widgets/widgets/qcommandlinkbutton.cpp +++ b/src/widgets/widgets/qcommandlinkbutton.cpp @@ -321,14 +321,12 @@ void QCommandLinkButton::paintEvent(QPaintEvent *) { Q_D(QCommandLinkButton); QStylePainter p(this); - p.save(); QStyleOptionButton option; initStyleOption(&option); option.text = QString(); option.icon = QIcon(); //we draw this ourselves - QSize pixmapSize = icon().actualSize(iconSize()); const int vOffset = isDown() ? style()->pixelMetric(QStyle::PM_ButtonShiftVertical, &option, this) : 0; @@ -337,10 +335,14 @@ void QCommandLinkButton::paintEvent(QPaintEvent *) //Draw icon p.drawControl(QStyle::CE_PushButton, option); - if (!icon().isNull()) - p.drawPixmap(d->leftMargin() + hOffset, d->topMargin() + vOffset, - icon().pixmap(pixmapSize, isEnabled() ? QIcon::Normal : QIcon::Disabled, - isChecked() ? QIcon::On : QIcon::Off)); + if (!icon().isNull()) { + const auto size = icon().actualSize(iconSize()); + const auto mode = isEnabled() ? QIcon::Normal : QIcon::Disabled; + const auto state = isChecked() ? QIcon::On : QIcon::Off; + const auto rect = QRect(d->leftMargin() + hOffset, d->topMargin() + vOffset, + size.width(), size.height()); + icon().paint(&p, rect, Qt::AlignCenter, mode, state); + } //Draw title QColor textColor = palette().buttonText().color(); @@ -366,7 +368,6 @@ void QCommandLinkButton::paintEvent(QPaintEvent *) p.setFont(d->descriptionFont()); p.drawItemText(d->descriptionRect().translated(hOffset, vOffset), textflags, option.palette, isEnabled(), description(), QPalette::ButtonText); - p.restore(); } void QCommandLinkButton::setDescription(const QString &description)