summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorEirik Aavitsland <eirik.aavitsland@qt.io>2022-06-20 16:33:55 +0200
committerQt Cherry-pick Bot <cherrypick_bot@qt-project.org>2022-08-10 10:45:41 +0000
commit9e76c0b01e8c99709347a3f08bc7566c0a28f1b7 (patch)
tree539572d6efd3b1bc4a3b99a0d819b9dfa4b53910
parenta4157f8b70622e2e161e67fbfab2223222cdf94c (diff)
Ensure consistent cursor width under fractional scaling
Under fractional scaling, an N units wide rectangle can in general cover either M or M+1 pixels, depending on placement. For a tall thin recangle like the cursor, this difference becomes very visible as the cursor moves from position to position. Avoid by instead painting the cursor as a cosmetic line in such cases, since that keeps its width independently of the current transformation. Fixes: QTBUG-95319 Change-Id: I31a31f89fe7eac3037694946aa452a9f2bd6e5be Reviewed-by: Morten Johan Sørvig <morten.sorvig@qt.io> (cherry picked from commit 3709bc3699ef0632bd2af53b02d44d130b8c0e13) Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
-rw-r--r--src/gui/text/qtextlayout.cpp15
1 files changed, 14 insertions, 1 deletions
diff --git a/src/gui/text/qtextlayout.cpp b/src/gui/text/qtextlayout.cpp
index aa65ce27cb..b263c9c00a 100644
--- a/src/gui/text/qtextlayout.cpp
+++ b/src/gui/text/qtextlayout.cpp
@@ -1327,7 +1327,20 @@ void QTextLayout::drawCursor(QPainter *p, const QPointF &pos, int cursorPosition
QPainter::CompositionMode origCompositionMode = p->compositionMode();
if (p->paintEngine()->hasFeature(QPaintEngine::RasterOpModes))
p->setCompositionMode(QPainter::RasterOp_NotDestination);
- p->fillRect(QRectF(x, y, qreal(width), (base + cursorDescent).toReal()), p->pen().brush());
+ const QTransform &deviceTransform = p->deviceTransform();
+ const qreal xScale = deviceTransform.m11();
+ if (deviceTransform.type() != QTransform::TxScale || std::trunc(xScale) == xScale) {
+ p->fillRect(QRectF(x, y, qreal(width), (base + cursorDescent).toReal()), p->pen().brush());
+ } else {
+ // Ensure consistently rendered cursor width under fractional scaling
+ const QPen origPen = p->pen();
+ QPen pen(origPen.brush(), qRound(width * xScale), Qt::SolidLine, Qt::FlatCap);
+ pen.setCosmetic(true);
+ const qreal center = x + qreal(width) / 2;
+ p->setPen(pen);
+ p->drawLine(QPointF(center, y), QPointF(center, y + (base + cursorDescent).toReal()));
+ p->setPen(origPen);
+ }
p->setCompositionMode(origCompositionMode);
if (toggleAntialiasing)
p->setRenderHint(QPainter::Antialiasing, false);