From f4093e05136a65c6812815706f0041d2b2d88a9e Mon Sep 17 00:00:00 2001 From: ChunLin Wang Date: Tue, 17 Aug 2021 20:07:31 +0800 Subject: Fix QScroller::scrollTo failing in QGraphicsView with movable item When forcing software scrolling through QScroller::scrollTo, it will start from (0, 0). QGraphicsViewPrivate::canStartScrollingAt should consider the locationof points, not just the flags of item. Fixes: QTBUG-70255 Change-Id: Iebdd5568baa3bdb41c705204dadb2895cfe9c0e2 Reviewed-by: Richard Moe Gustavsen (cherry picked from commit 9ba4c6beeff394d8526503b43ba471d82c27df9c) Reviewed-by: Qt Cherry-pick Bot --- src/widgets/graphicsview/qgraphicsview.cpp | 2 +- .../qgraphicsview/tst_qgraphicsview.cpp | 30 ++++++++++++++++++++++ 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/src/widgets/graphicsview/qgraphicsview.cpp b/src/widgets/graphicsview/qgraphicsview.cpp index 847d5fd45c..c0c659e8fd 100644 --- a/src/widgets/graphicsview/qgraphicsview.cpp +++ b/src/widgets/graphicsview/qgraphicsview.cpp @@ -614,7 +614,7 @@ bool QGraphicsViewPrivate::canStartScrollingAt(const QPoint &startPos) const const QGraphicsItem *childItem = q->itemAt(startPos); - if (childItem && (childItem->flags() & QGraphicsItem::ItemIsMovable)) + if (!startPos.isNull() && childItem && (childItem->flags() & QGraphicsItem::ItemIsMovable)) return false; return QAbstractScrollAreaPrivate::canStartScrollingAt(startPos); diff --git a/tests/auto/widgets/graphicsview/qgraphicsview/tst_qgraphicsview.cpp b/tests/auto/widgets/graphicsview/qgraphicsview/tst_qgraphicsview.cpp index bbcef8849e..3e3a1d18a4 100644 --- a/tests/auto/widgets/graphicsview/qgraphicsview/tst_qgraphicsview.cpp +++ b/tests/auto/widgets/graphicsview/qgraphicsview/tst_qgraphicsview.cpp @@ -50,6 +50,7 @@ #include #include #include +#include #if QT_CONFIG(opengl) #include #endif @@ -266,6 +267,7 @@ private slots: void QTBUG_5859_exposedRect(); void hoverLeave(); void QTBUG_16063_microFocusRect(); + void QTBUG_70255_scrollTo(); #ifndef QT_NO_CURSOR void QTBUG_7438_cursor(); #endif @@ -5025,5 +5027,33 @@ void tst_QGraphicsView::QTBUG_16063_microFocusRect() QCOMPARE(mfv, IMItem::mf.translated(-view.mapToScene(view.sceneRect().toRect()).boundingRect().topLeft())); } +void tst_QGraphicsView::QTBUG_70255_scrollTo() +{ + QGraphicsView view; + QGraphicsScene scene; + view.setFixedSize(200, 200); + scene.setSceneRect(0, 0, 1000, 1000); + QGraphicsRectItem item; + item.setRect(-20, -20, 40, 40); + item.setFlag(QGraphicsItem::ItemIsMovable, true); + scene.addItem(&item); + view.setScene(&scene); + view.centerOn(0, 0); + + view.show(); + QApplication::setActiveWindow(&view); + if (!QTest::qWaitForWindowExposed(&view) || !QTest::qWaitForWindowActive(&view)) + QSKIP("Failed to show and activate window"); + + QPoint point = view.mapFromScene(0, 0); + QCOMPARE(point, QPoint(0, 0)); + + QScroller::scroller(&view)->scrollTo(QPointF(0, 500), 100); + QTest::qWait(200); + + point = view.mapFromScene(0, 0); + QCOMPARE(point, QPoint(0, -500)); +} + QTEST_MAIN(tst_QGraphicsView) #include "tst_qgraphicsview.moc" -- cgit v1.2.3