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>
This commit is contained in:
Eirik Aavitsland 2022-06-20 16:33:55 +02:00 committed by Qt Cherry-pick Bot
parent c064b19b24
commit 8eedaf8a87

View File

@ -1291,7 +1291,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);