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.

Change-Id: I7e2492a09b28cb8a4f4cc60454733e0054fe1e9b
Reviewed-by: Volker Hilsheimer <volker.hilsheimer@qt.io>
(cherry picked from commit 785fb89e0aafc25a435d28ff05bf0dc05385b372)
Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
(cherry picked from commit 6790d8d716d330993d892b5df14b3e3cb6b8343a)
This commit is contained in:
Christian Ehrlicher 2024-12-24 14:16:00 +01:00 committed by Qt Cherry-pick Bot
parent 7c0fe044ec
commit f72431d0f9

View File

@ -315,14 +315,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;
@ -331,10 +329,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();
@ -360,7 +362,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)