diff options
author | J-P Nurmi <jpnurmi@qt.io> | 2018-04-23 14:05:25 +0200 |
---|---|---|
committer | J-P Nurmi <jpnurmi@qt.io> | 2018-04-23 19:37:49 +0000 |
commit | def92f7b657ee9247beffffcb0cadd1eca8be3c6 (patch) | |
tree | c804d09050f9ba05a47aa6d27f652261b24f7f5f | |
parent | ec0ad4ca9207acbc524a109ffbacb6cbb0fa18a8 (diff) |
ComboBox: don't block the escape/back key
Accept Key_Escape or Key_Back only if it actually close the popup. If
the popup is not visible, the key is not handled, and the event should
therefore propagate (and potentially close the app on Android).
Change-Id: Ibdb0cab8e0ac47005c5112f7ca4882288f1f5a17
Task-number: QTBUG-67684
Reviewed-by: Mitch Curtis <mitch.curtis@qt.io>
-rw-r--r-- | src/quicktemplates2/qquickcombobox.cpp | 8 | ||||
-rw-r--r-- | tests/auto/controls/data/tst_combobox.qml | 71 |
2 files changed, 76 insertions, 3 deletions
diff --git a/src/quicktemplates2/qquickcombobox.cpp b/src/quicktemplates2/qquickcombobox.cpp index d3226baf..4d5efece 100644 --- a/src/quicktemplates2/qquickcombobox.cpp +++ b/src/quicktemplates2/qquickcombobox.cpp @@ -1624,9 +1624,11 @@ void QQuickComboBox::keyReleaseEvent(QKeyEvent *event) break; case Qt::Key_Escape: case Qt::Key_Back: - d->hidePopup(false); - setPressed(false); - event->accept(); + if (d->isPopupVisible()) { + d->hidePopup(false); + setPressed(false); + event->accept(); + } break; default: break; diff --git a/tests/auto/controls/data/tst_combobox.qml b/tests/auto/controls/data/tst_combobox.qml index 801712be..7f8bc4e8 100644 --- a/tests/auto/controls/data/tst_combobox.qml +++ b/tests/auto/controls/data/tst_combobox.qml @@ -1600,4 +1600,75 @@ TestCase { tryCompare(control.popup, "visible", true) compare(control.popup.height, control.popup.topPadding + control.popup.bottomPadding) } + + Component { + id: keysMonitor + Item { + property int pressedKeys: 0 + property int releasedKeys: 0 + property int lastPressedKey: 0 + property int lastReleasedKey: 0 + property alias comboBox: comboBox + + width: 200 + height: 200 + + Keys.onPressed: { ++pressedKeys; lastPressedKey = event.key } + Keys.onReleased: { ++releasedKeys; lastReleasedKey = event.key } + + ComboBox { + id: comboBox + } + } + } + + function test_keyClose_data() { + return [ + { tag: "Escape", key: Qt.Key_Escape }, + { tag: "Back", key: Qt.Key_Back } + ] + } + + function test_keyClose(data) { + var container = createTemporaryObject(keysMonitor, testCase) + verify(container) + + var control = comboBox.createObject(container) + verify(control) + + control.forceActiveFocus() + verify(control.activeFocus) + + // popup not visible -> propagates + keyPress(data.key) + compare(container.pressedKeys, 1) + compare(container.lastPressedKey, data.key) + + keyRelease(data.key) + compare(container.releasedKeys, 1) + compare(container.lastReleasedKey, data.key) + + verify(control.activeFocus) + + // popup visible -> handled -> does not propagate + control.popup.open() + tryCompare(control.popup, "opened", true) + + keyPress(data.key) + compare(container.pressedKeys, 1) + + keyRelease(data.key) + compare(container.releasedKeys, 2) // ### TODO: should Popup block the key release? + + verify(control.activeFocus) + + // popup not visible -> propagates + keyPress(data.key) + compare(container.pressedKeys, 2) + compare(container.lastPressedKey, data.key) + + keyRelease(data.key) + compare(container.releasedKeys, 3) + compare(container.lastReleasedKey, data.key) + } } |