A testcase like the attached one, which sends a mouse event to a QWidget using
QTest::mouseClick, used to adjust QApplication::keyboardModifiers() in Qt4,
but doesn't do so anymore in Qt5.

It seems the code that changes modifier_buttons is now in 
QGuiApplicationPrivate::processMouseEvent, which is not called by 
QTest::mouseClick.

qapplication.cpp:2983 (which is called) says
    // capture the current mouse/keyboard state
but it only captures the mouse state, not the keyboard state.

Should it capture keyboard state again, like in Qt4?

But the interesting thing in all this is that outside of unit tests, it's all 
working fine (when I click on an actual widget, 
QApplication::keyboardModifiers() is set). 
So maybe the real fix would be that QTest::mouseClick works more like a real 
mouse click, going via the QWindow first, before being sent to the
target QWidget? Not sure what this means exactly in terms of code changes 
though.

-- 
David Faure, fa...@kde.org, http://www.davidfaure.fr
Sponsored by Nokia to work on KDE, incl. KDE Frameworks 5
diff --git a/tests/auto/widgets/kernel/qwidget/tst_qwidget.cpp b/tests/auto/widgets/kernel/qwidget/tst_qwidget.cpp
index 1b4cacf..14a856d 100644
--- a/tests/auto/widgets/kernel/qwidget/tst_qwidget.cpp
+++ b/tests/auto/widgets/kernel/qwidget/tst_qwidget.cpp
@@ -387,6 +387,8 @@ private slots:
 
     void touchEventSynthesizedMouseEvent();
 
+    void keyboardModifiers();
+
 private:
     bool ensureScreenSize(int width, int height);
     QWidget *testWidget;
@@ -9443,5 +9445,28 @@ void tst_QWidget::touchEventSynthesizedMouseEvent()
     }
 }
 
+class KeyboardWidget : public QWidget
+{
+public:
+    KeyboardWidget(QWidget* parent = 0) : QWidget(parent), m_eventCounter(0) {}
+    virtual void mousePressEvent(QMouseEvent* ev) Q_DECL_OVERRIDE {
+        m_modifiers = ev->modifiers();
+        m_appModifiers = QApplication::keyboardModifiers();
+        ++m_eventCounter;
+    }
+    Qt::KeyboardModifiers m_modifiers;
+    Qt::KeyboardModifiers m_appModifiers;
+    int m_eventCounter;
+};
+
+void tst_QWidget::keyboardModifiers()
+{
+    KeyboardWidget* w = new KeyboardWidget;
+    QTest::mouseClick(w, Qt::LeftButton, Qt::ControlModifier);
+    QCOMPARE(w->m_eventCounter, 1);
+    QCOMPARE(int(w->m_modifiers), int(Qt::ControlModifier));
+    QCOMPARE(int(w->m_appModifiers), int(Qt::ControlModifier));
+}
+
 QTEST_MAIN(tst_QWidget)
 #include "tst_qwidget.moc"
_______________________________________________
Development mailing list
Development@qt-project.org
http://lists.qt-project.org/mailman/listinfo/development

Reply via email to