From 0dbc575c1a8359534761167a5f5f1e29abedd51d Mon Sep 17 00:00:00 2001 From: Laszlo Agocs Date: Wed, 22 Feb 2017 16:16:14 +0100 Subject: Make keyboard events work in QQuickWidget Right now many cases outside of text input fields are simply broken, e.g. one cannot use a TableView or other controls with the keyboard. Removing the seemingly unnecessary focusObject() solves all problems since this way key events get delivered to the QQuickWidget which in turn forwards to the QQuickWindow. Directly routing into the QQuickWindow's focusObject(), which can be any item in the scene is wrong since it skips a big part of QQuickWindow's event handling logic. Task-number: QTBUG-45757 Change-Id: Ie53b9003d156ab019fa4b9cf461e209990e738f7 Reviewed-by: Paul Olav Tvete --- .../quickwidgets/qquickwidget/tst_qquickwidget.cpp | 28 ++++++++++++++++++++++ 1 file changed, 28 insertions(+) (limited to 'tests/auto/quickwidgets') diff --git a/tests/auto/quickwidgets/qquickwidget/tst_qquickwidget.cpp b/tests/auto/quickwidgets/qquickwidget/tst_qquickwidget.cpp index 5e8f762e23..bd051ec990 100644 --- a/tests/auto/quickwidgets/qquickwidget/tst_qquickwidget.cpp +++ b/tests/auto/quickwidgets/qquickwidget/tst_qquickwidget.cpp @@ -58,6 +58,7 @@ private slots: void grabBeforeShow(); void reparentToNewWindow(); void nullEngine(); + void keyEvents(); }; @@ -337,6 +338,33 @@ void tst_qquickwidget::nullEngine() QVERIFY(widget.engine()); } +class KeyHandlingWidget : public QQuickWidget +{ +public: + void keyPressEvent(QKeyEvent *e) override { + if (e->key() == Qt::Key_A) + ok = true; + } + + bool ok = false; +}; + +void tst_qquickwidget::keyEvents() +{ + // A QQuickWidget should behave like a normal widget when it comes to event handling. + // Verify that key events actually reach the widget. (QTBUG-45757) + KeyHandlingWidget widget; + widget.setSource(testFileUrl("rectangle.qml")); + widget.show(); + QVERIFY(QTest::qWaitForWindowExposed(widget.window(), 5000)); + + // Note: send the event to the QWindow, not the QWidget, in order + // to simulate the full event processing chain. + QTest::keyClick(widget.window()->windowHandle(), Qt::Key_A); + + QTRY_VERIFY(widget.ok); +} + QTEST_MAIN(tst_qquickwidget) #include "tst_qquickwidget.moc" -- cgit v1.2.3