diff options
| author | Christian Ehrlicher <[email protected]> | 2025-10-13 22:55:18 +0200 |
|---|---|---|
| committer | Christian Ehrlicher <[email protected]> | 2025-10-22 06:12:37 +0200 |
| commit | 8e64635edc9026543d2da99cbddb84a962bc34d7 (patch) | |
| tree | e6fe3a97c019c3f6761b6f3254fe10a8e4ed7ef0 | |
| parent | b252b715c14da194c41e802319933f562e736082 (diff) | |
QGraphicsView tests: fix unittests for 'windows' style
QGraphicsView::mapFromScene() returns the coordinates in viewport
coordinates, the tests use them to trigger events in the view. Since the
view might contain a frame around the viewport, the coordinates differ
depending on the value of PM_DefaultFrameWidth. Some tests already had a
fuzzing compare to not fail for a frame width of 1 but this does not
work for a larger value.
Some tests also set a fixed size for the view where the frame width was
not considered which lead to unwanted scrollbars.
Fixes: QTBUG-141079
Change-Id: If09898e08d5e5558825db461f02b6847ae90fa29
Reviewed-by: Richard Moe Gustavsen <[email protected]>
| -rw-r--r-- | tests/auto/widgets/graphicsview/qgraphicsproxywidget/tst_qgraphicsproxywidget.cpp | 63 | ||||
| -rw-r--r-- | tests/auto/widgets/graphicsview/qgraphicsview/tst_qgraphicsview.cpp | 99 |
2 files changed, 85 insertions, 77 deletions
diff --git a/tests/auto/widgets/graphicsview/qgraphicsproxywidget/tst_qgraphicsproxywidget.cpp b/tests/auto/widgets/graphicsview/qgraphicsproxywidget/tst_qgraphicsproxywidget.cpp index 17acdea014e..2f0cbf78b25 100644 --- a/tests/auto/widgets/graphicsview/qgraphicsproxywidget/tst_qgraphicsproxywidget.cpp +++ b/tests/auto/widgets/graphicsview/qgraphicsproxywidget/tst_qgraphicsproxywidget.cpp @@ -25,6 +25,7 @@ #include <QtWidgets/qtablewidget.h> #include <QtGui/qevent.h> +#include <QtGui/qstylehints.h> #include <QtGui/private/qhighdpiscaling_p.h> #include <QtCore/qmimedata.h> @@ -3333,7 +3334,7 @@ void tst_QGraphicsProxyWidget::clickFocus() EventSpy proxySpy(proxy); EventSpy widgetSpy(proxy->widget()); - view.setFrameStyle(0); + view.setFrameStyle(QFrame::NoFrame); view.resize(300, 300); view.show(); QVERIFY(QTest::qWaitForWindowFocused(&view)); @@ -3469,10 +3470,12 @@ void tst_QGraphicsProxyWidget::QTBUG_6986_sendMouseEventToAlienWidget() auto *hoverButton = new HoverButton(background); hoverButton->setText("Second button"_L1); hoverButton->setGeometry(10, 10, 200, 50); + hoverButton->setAttribute(Qt::WA_Hover, true); scene.addWidget(background); auto *hideButton = new QPushButton("I'm a button with a very very long text"_L1); hideButton->setGeometry(10, 10, 400, 50); + hideButton->setAttribute(Qt::WA_Hover, true); QGraphicsProxyWidget *topButton = scene.addWidget(hideButton); connect(hideButton, &QPushButton::clicked, &scene, [&]() { topButton->hide(); }); topButton->setFocus(); @@ -3659,6 +3662,11 @@ public: return QWidget::event(event); } + void paintEvent(QPaintEvent *) override + { + QPainter p(this); + p.fillRect(rect(), Qt::green); + } }; #if QT_CONFIG(wheelevent) @@ -3676,6 +3684,11 @@ public: */ void tst_QGraphicsProxyWidget::wheelEventPropagation() { + qApp->styleHints()->setWheelScrollLines(3); + const auto guard = qScopeGuard([&]() { + qApp->styleHints()->setWheelScrollLines(-1); + }); + QGraphicsScene scene(0, 0, 600, 600); auto *label = new QLabel("Direct"_L1); @@ -3697,6 +3710,11 @@ void tst_QGraphicsProxyWidget::wheelEventPropagation() int wheelEventCount = 0; protected: + void paintEvent(QPaintEvent *) override + { + QPainter p(this); + p.fillRect(rect(), Qt::green); + } void wheelEvent(QWheelEvent *) override { ++wheelEventCount; @@ -3710,10 +3728,13 @@ void tst_QGraphicsProxyWidget::wheelEventPropagation() QGraphicsView view(&scene); view.setFixedHeight(200); + view.setFixedWidth(700); + view.setFrameStyle(QFrame::NoFrame); view.show(); QVERIFY(QTest::qWaitForWindowActive(&view)); QVERIFY(view.verticalScrollBar()->isVisible()); + QVERIFY(!view.horizontalScrollBar()->isVisible()); view.verticalScrollBar()->setValue(0); QSignalSpy scrollSpy(view.verticalScrollBar(), &QScrollBar::valueChanged); @@ -3729,6 +3750,8 @@ void tst_QGraphicsProxyWidget::wheelEventPropagation() phase); QCoreApplication::processEvents(); }; + auto vs = view.verticalScrollBar(); + vs->setSingleStep(9); // each wheel event: wheelScrollLines() * 9 = 27px qsizetype scrollCount = 0; // test non-kinetic events; they are not grabbed, and should scroll the view unless @@ -3816,6 +3839,7 @@ void tst_QGraphicsProxyWidget::forwardTouchEvent() QGraphicsView view(&scene); view.show(); + view.setFrameStyle(QFrame::NoFrame); QVERIFY(QTest::qWaitForWindowActive(&view)); EventSpy eventSpy(widget); @@ -3832,16 +3856,14 @@ void tst_QGraphicsProxyWidget::forwardTouchEvent() QTest::touchEvent(&view, device.get()).move(0, QPoint(16, 16), &view); QTest::touchEvent(&view, device.get()).release(0, QPoint(15, 15), &view); - QApplication::processEvents(); - - QCOMPARE(eventSpy.counts[QEvent::TouchBegin], 1); - QCOMPARE(eventSpy.counts[QEvent::TouchUpdate], 2); - QCOMPARE(eventSpy.counts[QEvent::TouchEnd], 1); + QTRY_COMPARE(eventSpy.counts[QEvent::TouchBegin], 1); + QTRY_COMPARE(eventSpy.counts[QEvent::TouchUpdate], 2); + QTRY_COMPARE(eventSpy.counts[QEvent::TouchEnd], 1); } void tst_QGraphicsProxyWidget::touchEventPropagation() { - QGraphicsScene scene(0, 0, 300, 200); + QGraphicsScene scene; auto *simpleWidget = new QWidget; simpleWidget->setObjectName("simpleWidget"); simpleWidget->setAttribute(Qt::WA_AcceptTouchEvents, true); @@ -3873,15 +3895,26 @@ void tst_QGraphicsProxyWidget::touchEventPropagation() vbox->addWidget(touchWidget2); QGraphicsProxyWidget *formProxy = scene.addWidget(formWidget); formProxy->setAcceptTouchEvents(true); - formProxy->setGeometry(QRectF(50, 50, 200, 160)); + const auto minSize = formWidget->minimumSize(); + formProxy->setGeometry(QRectF(50, 50, minSize.width(), minSize.height())); + + // topLeft must be 0/0 + const auto sceneRect = scene.sceneRect(); + scene.setSceneRect(QRectF(0, 0, sceneRect.width(), sceneRect.height())); QGraphicsView view(&scene); + // by setting NoFrame, view and view.viewport() have the same + // coordinate system + view.setFrameStyle(QFrame::NoFrame); + // make sure to not have scrollbars view.setFixedSize(scene.width(), scene.height()); view.verticalScrollBar()->setValue(0); view.horizontalScrollBar()->setValue(0); view.viewport()->setObjectName("GraphicsView's Viewport"); view.show(); QVERIFY(QTest::qWaitForWindowExposed(&view)); + QVERIFY(!view.horizontalScrollBar()->isVisible()); + QVERIFY(!view.verticalScrollBar()->isVisible()); class TouchEventSpy : public QObject { @@ -3913,8 +3946,12 @@ void tst_QGraphicsProxyWidget::touchEventPropagation() case QEvent::TouchEnd: { auto *touchEvent = static_cast<QTouchEvent *>(event); // instead of detaching each QEventPoint, just store the relative positions - for (const auto &touchPoint : touchEvent->points()) - records[touchPoint.id()] << TouchRecord{receiver, event->type(), touchPoint.position()}; + for (const auto& touchPoint : touchEvent->points()) { + records[touchPoint.id()] + << TouchRecord{ receiver, event->type(), touchPoint.position() }; + qCDebug(lcTests) << touchPoint.id() << "recv:" << receiver << "type" + << event->type() << "pos" << touchPoint.position(); + } qCDebug(lcTests) << "Recording" << event << receiver; break; } @@ -3944,9 +3981,9 @@ void tst_QGraphicsProxyWidget::touchEventPropagation() }; // verify that the embedded widget gets the correctly translated event - QTest::touchEvent(&view, touchDevice.get()).press(0, simpleCenter.toPoint()); + QTest::touchEvent(&view, touchDevice.get()).press(0, view.mapFromScene(simpleCenter)); // window, viewport, scene, simpleProxy, simpleWidget - QCOMPARE(eventSpy.count(), 5); + QTRY_COMPARE(eventSpy.count(), 5); QCOMPARE(eventSpy.at(0).receiver, view.windowHandle()); QCOMPARE(eventSpy.at(1).receiver, view.viewport()); QCOMPARE(eventSpy.at(2).receiver, &scene); @@ -3969,7 +4006,7 @@ void tst_QGraphicsProxyWidget::touchEventPropagation() QCOMPARE(formWidget->childAt(touchWidget2->pos() + tw2Center), touchWidget2); // touch events are sent to the view, in view coordinates - const QPoint formProxyPox = view.mapFromScene(formProxy->pos().toPoint()); + const QPoint formProxyPox = view.mapFromScene(formProxy->pos()); const QPoint pb1TouchPos = pushButton1->pos() + pb1Center + formProxyPox; const QPoint pb2TouchPos = pushButton2->pos() + pb2Center + formProxyPox; const QPoint tw1TouchPos = touchWidget1->pos() + tw1Center + formProxyPox; diff --git a/tests/auto/widgets/graphicsview/qgraphicsview/tst_qgraphicsview.cpp b/tests/auto/widgets/graphicsview/qgraphicsview/tst_qgraphicsview.cpp index df19ea1568e..48310996c46 100644 --- a/tests/auto/widgets/graphicsview/qgraphicsview/tst_qgraphicsview.cpp +++ b/tests/auto/widgets/graphicsview/qgraphicsview/tst_qgraphicsview.cpp @@ -2058,28 +2058,21 @@ void tst_QGraphicsView::mapFromSceneRect() QWidget topLevel; QGraphicsView view(&scene,&topLevel); view.rotate(90); - view.setFixedSize(200, 200); - view.setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff); - view.setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff); + const auto fw = view.frameWidth() * 2; + view.setFixedSize(200 + fw, 200 + fw); topLevel.show(); - QVERIFY(QTest::qWaitForWindowActive(&view)); + QVERIFY(QTest::qWaitForWindowActive(&topLevel)); + QVERIFY(!view.horizontalScrollBar()->isVisible()); + QVERIFY(!view.verticalScrollBar()->isVisible()); - QPolygon polygon; - polygon << QPoint(98, 98); - polygon << QPoint(98, 108); - polygon << QPoint(88, 108); - polygon << QPoint(88, 98); - - - QPolygon viewPolygon = view.mapFromScene(0, 0, 10, 10); - for (int i = 0; i < 4; ++i) { - QVERIFY(qAbs(viewPolygon[i].x() - polygon[i].x()) < 3); - QVERIFY(qAbs(viewPolygon[i].y() - polygon[i].y()) < 3); - } + const QRectF input(0, 0, 10, 10); + // fixed size is 200x200 so center is 100x100 + const QPolygon polygon{ QPoint(100, 100), QPoint(100, 110), QPoint(90, 110), QPoint(90, 100) }; + const QPolygon viewPolygon = view.mapFromScene(input); + QCOMPARE(viewPolygon, polygon); QPoint pt = view.mapFromScene(QPointF()); - QPolygon p; - p << pt << pt << pt << pt; + QPolygon p{ pt, pt, pt, pt }; QCOMPARE(view.mapFromScene(QRectF()), p); } @@ -2088,28 +2081,18 @@ void tst_QGraphicsView::mapFromScenePoly() QGraphicsScene scene; QGraphicsView view(&scene); view.rotate(90); - view.setFixedSize(200, 200); - view.setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff); - view.setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff); + const auto fw = view.frameWidth() * 2; + view.setFixedSize(200 + fw, 200 + fw); view.show(); - - QPolygonF polygon; - polygon << QPoint(0, 0); - polygon << QPoint(10, 0); - polygon << QPoint(10, 10); - polygon << QPoint(0, 10); - - QPolygon polygon2; - polygon2 << QPoint(98, 98); - polygon2 << QPoint(98, 108); - polygon2 << QPoint(88, 108); - polygon2 << QPoint(88, 98); - - QPolygon viewPolygon = view.mapFromScene(polygon); - for (int i = 0; i < 4; ++i) { - QVERIFY(qAbs(viewPolygon[i].x() - polygon2[i].x()) < 3); - QVERIFY(qAbs(viewPolygon[i].y() - polygon2[i].y()) < 3); - } + QVERIFY(QTest::qWaitForWindowActive(&view)); + QVERIFY(!view.horizontalScrollBar()->isVisible()); + QVERIFY(!view.verticalScrollBar()->isVisible()); + + const QPolygonF input{ QPoint(0, 0), QPoint(10, 0), QPoint(10, 10), QPoint(0, 10) }; + // fixed size is 200x200 so center is 100x100 + const QPolygon polygon{ QPoint(100, 100), QPoint(100, 110), QPoint(90, 110), QPoint(90, 100) }; + const QPolygon viewPolygon = view.mapFromScene(input); + QCOMPARE(viewPolygon, polygon); } void tst_QGraphicsView::mapFromScenePath() @@ -2117,34 +2100,22 @@ void tst_QGraphicsView::mapFromScenePath() QGraphicsScene scene; QGraphicsView view(&scene); view.rotate(90); - view.setFixedSize(200, 200); - view.setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff); - view.setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff); + const auto fw = view.frameWidth() * 2; + view.setFixedSize(200 + fw, 200 + fw); view.show(); - - QPolygonF polygon; - polygon << QPoint(0, 0); - polygon << QPoint(10, 0); - polygon << QPoint(10, 10); - polygon << QPoint(0, 10); + QVERIFY(QTest::qWaitForWindowActive(&view)); + QVERIFY(!view.horizontalScrollBar()->isVisible()); + QVERIFY(!view.verticalScrollBar()->isVisible()); + + const QPolygonF input{ QPoint(0, 0), QPoint(10, 0), QPoint(10, 10), QPoint(0, 10) }; + QPainterPath inputPath; + inputPath.addPolygon(input); + // fixed size is 200x200 so center is 100x100 + const QPolygon polygon{ QPoint(100, 100), QPoint(100, 110), QPoint(90, 110), QPoint(90, 100) }; QPainterPath path; path.addPolygon(polygon); - - QPolygon polygon2; - polygon2 << QPoint(98, 98); - polygon2 << QPoint(98, 108); - polygon2 << QPoint(88, 108); - polygon2 << QPoint(88, 98); - QPainterPath path2; - path2.addPolygon(polygon2); - - QPolygonF pathPoly = view.mapFromScene(path).toFillPolygon(); - QPolygonF path2Poly = path2.toFillPolygon(); - - for (int i = 0; i < pathPoly.size(); ++i) { - QVERIFY(qAbs(pathPoly[i].x() - path2Poly[i].x()) < 3); - QVERIFY(qAbs(pathPoly[i].y() - path2Poly[i].y()) < 3); - } + QPainterPath viewPath = view.mapFromScene(inputPath); + QCOMPARE(viewPath, path); } void tst_QGraphicsView::sendEvent() |
