From 4f1752a6b9072083ee8408c4cc7f3e821c9661a6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thorbj=C3=B8rn=20Lindeijer?= Date: Wed, 10 Jan 2024 14:21:52 +0100 Subject: [PATCH] Increase precision for QGraphicsView::AnchorUnderMouse * Use a more precise view center for views with odd width/height * Use the QPointF version of mapToScene to avoid rounding * Round instead of truncate when setting scroll bar values These changes increase the precision of AnchorUnderMouse, which is important when for example wheel scrolling is used to change the scale of the view. Without these changes, the view shifts slightly with each change in the transform. [ChangeLog][QtWidgets][QGraphicsView] Increase precision for QGraphicsView::AnchorUnderMouse and QGraphicsView::centerOn Pick-to: 6.6 Task-number: QTBUG-96879 Change-Id: I8199196c671e4aa96732f382e8057468f676b8d7 Reviewed-by: Eirik Aavitsland (cherry picked from commit d99b0cfed21e05f6e84b97fe8edb68271a34deb2) Reviewed-by: Qt Cherry-pick Bot --- src/widgets/graphicsview/qgraphicsview.cpp | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/src/widgets/graphicsview/qgraphicsview.cpp b/src/widgets/graphicsview/qgraphicsview.cpp index 1dc66eb5105..9505e2529aa 100644 --- a/src/widgets/graphicsview/qgraphicsview.cpp +++ b/src/widgets/graphicsview/qgraphicsview.cpp @@ -483,8 +483,8 @@ void QGraphicsViewPrivate::centerView(QGraphicsView::ViewportAnchor anchor) if (q->underMouse()) { // Last scene pos: lastMouseMoveScenePoint // Current mouse pos: - QPointF transformationDiff = q->mapToScene(viewport->rect().center()) - - q->mapToScene(viewport->mapFromGlobal(QCursor::pos())); + QPointF transformationDiff = mapToScene(viewport->rect().toRectF().center()) + - mapToScene(viewport->mapFromGlobal(QCursor::pos().toPointF())); q->centerOn(lastMouseMoveScenePoint + transformationDiff); } else { q->centerOn(lastCenterPoint); @@ -504,8 +504,7 @@ void QGraphicsViewPrivate::centerView(QGraphicsView::ViewportAnchor anchor) */ void QGraphicsViewPrivate::updateLastCenterPoint() { - Q_Q(QGraphicsView); - lastCenterPoint = q->mapToScene(viewport->rect().center()); + lastCenterPoint = mapToScene(viewport->rect().toRectF().center()); } /*! @@ -1892,14 +1891,14 @@ void QGraphicsView::centerOn(const QPointF &pos) qint64 horizontal = 0; horizontal += horizontalScrollBar()->minimum(); horizontal += horizontalScrollBar()->maximum(); - horizontal -= int(viewPoint.x() - width / 2.0); + horizontal -= qRound(viewPoint.x() - width / 2.0); horizontalScrollBar()->setValue(horizontal); } else { - horizontalScrollBar()->setValue(int(viewPoint.x() - width / 2.0)); + horizontalScrollBar()->setValue(qRound(viewPoint.x() - width / 2.0)); } } if (!d->topIndent) - verticalScrollBar()->setValue(int(viewPoint.y() - height / 2.0)); + verticalScrollBar()->setValue(qRound(viewPoint.y() - height / 2.0)); d->lastCenterPoint = oldCenterPoint; }