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 <volker.hilsheimer@qt.io>
(cherry picked from commit 785fb89e0aafc25a435d28ff05bf0dc05385b372)
Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
This commit is contained in:
Christian Ehrlicher 2024-12-24 14:16:00 +01:00 committed by Qt Cherry-pick Bot
parent d39c493390
commit 6790d8d716

View File

@ -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)