From 0361ff89399a2d6327d51708ee9e16e84d30ff7c Mon Sep 17 00:00:00 2001 From: David Faure Date: Tue, 28 Nov 2023 18:07:44 +0100 Subject: [PATCH] QCommonStyle: draw focus rect using qDrawPlainRect A non-aliased drawRect(r.adjusted(0, 0, -1, -1)) no longer does the job in Qt6: hi-dpi is now enabled automatically, which scales the painter and leads to horrible (non symmetric) rendering. I doubt any actual widget style calls into this code (which draws a focus rect with a plain line) but it's still a useful example and fallback when writing a widget style. Change-Id: Ib407a7355033258be568b4826fe01c110f02c018 Reviewed-by: Christian Ehrlicher (cherry picked from commit 5ef5f2f1b9d90264df02277884e5e18f8c1fc783) Reviewed-by: Qt Cherry-pick Bot --- src/widgets/styles/qcommonstyle.cpp | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/widgets/styles/qcommonstyle.cpp b/src/widgets/styles/qcommonstyle.cpp index f5dd5396505..5498cdea651 100644 --- a/src/widgets/styles/qcommonstyle.cpp +++ b/src/widgets/styles/qcommonstyle.cpp @@ -189,20 +189,20 @@ void QCommonStyle::drawPrimitive(PrimitiveElement pe, const QStyleOption *opt, Q case PE_FrameFocusRect: if (const QStyleOptionFocusRect *fropt = qstyleoption_cast(opt)) { QColor bg = fropt->backgroundColor; - QPen oldPen = p->pen(); + QColor color; if (bg.isValid()) { int h, s, v; bg.getHsv(&h, &s, &v); if (v >= 128) - p->setPen(Qt::black); + color = Qt::black; else - p->setPen(Qt::white); + color = Qt::white; } else { - p->setPen(opt->palette.windowText().color()); + color = opt->palette.windowText().color(); } - QRect focusRect = opt->rect.adjusted(1, 1, -1, -1); - p->drawRect(focusRect.adjusted(0, 0, -1, -1)); //draw pen inclusive - p->setPen(oldPen); + const QRect focusRect = opt->rect.adjusted(1, 1, -1, -1); + const QBrush fill(bg); + qDrawPlainRect(p, focusRect, color, 1, bg.isValid() ? &fill : nullptr); } break; case PE_IndicatorMenuCheckMark: {