diff options
author | Jan Arve Sæther <jan-arve.saether@qt.io> | 2018-11-21 11:34:32 +0100 |
---|---|---|
committer | Frederik Gladhorn <frederik.gladhorn@qt.io> | 2018-12-13 17:53:19 +0000 |
commit | 0a92b02f1784a8934490633edcfed79fc685bfa2 (patch) | |
tree | 2686826b9c64dc7e760ae5a2a169b13658a48e0d | |
parent | 9929c250a3829aec82955da91413bc7673dc159d (diff) |
Fix Keys.onShortcutOverride for TextEdit
It was not possible to override a shortcut with Keys.onShortcutOverride
for TextEdit. This was because the ShortcutOverride event sent to the
TextEdit was not passed on to the default event() implementation (of the
base class) in the case that the QQuickTextEdit did not handle the
ShortcutOverride event.
Fixes: QTBUG-68711
Change-Id: I981738d8f92c77eadb8dd98b4245290d430525d3
Reviewed-by: Jan Arve Sæther <jan-arve.saether@qt.io>
Reviewed-by: Liang Qi <liang.qi@qt.io>
Reviewed-by: Frederik Gladhorn <frederik.gladhorn@qt.io>
-rw-r--r-- | src/quick/items/qquicktextedit.cpp | 3 | ||||
-rw-r--r-- | tests/auto/quick/qquicktextedit/data/keys_shortcutoverride.qml | 34 | ||||
-rw-r--r-- | tests/auto/quick/qquicktextedit/tst_qquicktextedit.cpp | 32 |
3 files changed, 68 insertions, 1 deletions
diff --git a/src/quick/items/qquicktextedit.cpp b/src/quick/items/qquicktextedit.cpp index 06a0fc396b..3a12ad6ba5 100644 --- a/src/quick/items/qquicktextedit.cpp +++ b/src/quick/items/qquicktextedit.cpp @@ -1641,7 +1641,8 @@ bool QQuickTextEdit::event(QEvent *event) Q_D(QQuickTextEdit); if (event->type() == QEvent::ShortcutOverride) { d->control->processEvent(event, QPointF(-d->xoff, -d->yoff)); - return event->isAccepted(); + if (event->isAccepted()) + return true; } return QQuickImplicitSizeItem::event(event); } diff --git a/tests/auto/quick/qquicktextedit/data/keys_shortcutoverride.qml b/tests/auto/quick/qquicktextedit/data/keys_shortcutoverride.qml new file mode 100644 index 0000000000..24bd434830 --- /dev/null +++ b/tests/auto/quick/qquicktextedit/data/keys_shortcutoverride.qml @@ -0,0 +1,34 @@ +import QtQuick 2.10 + +Item { + width: 320 + height: 200 + property string who : "nobody" + + Shortcut { + sequence: "Esc" + onActivated: who = "Shortcut" + } + + TextEdit { + id: txt + x: 100 + text: "enter text" + Keys.onShortcutOverride: { + who = "TextEdit" + event.accepted = (event.key === Qt.Key_Escape) + } + } + + Rectangle { + objectName: "rectangle" + width: 90 + height: width + focus: true + color: focus ? "red" : "gray" + Keys.onShortcutOverride: { + who = "Rectangle" + event.accepted = (event.key === Qt.Key_Escape) + } + } +} diff --git a/tests/auto/quick/qquicktextedit/tst_qquicktextedit.cpp b/tests/auto/quick/qquicktextedit/tst_qquicktextedit.cpp index da15ca6b48..f32da44daa 100644 --- a/tests/auto/quick/qquicktextedit/tst_qquicktextedit.cpp +++ b/tests/auto/quick/qquicktextedit/tst_qquicktextedit.cpp @@ -36,6 +36,7 @@ #include <QtQml/qqmlexpression.h> #include <QtQml/qqmlcomponent.h> #include <QtGui/qguiapplication.h> +#include <private/qquickrectangle_p.h> #include <private/qquicktextedit_p.h> #include <private/qquicktextedit_p_p.h> #include <private/qquicktext_p.h> @@ -205,6 +206,7 @@ private slots: void padding(); void QTBUG_51115_readOnlyResetsSelection(); + void keys_shortcutoverride(); private: void simulateKeys(QWindow *window, const QList<Key> &keys); @@ -5671,6 +5673,36 @@ void tst_qquicktextedit::QTBUG_51115_readOnlyResetsSelection() QCOMPARE(obj->selectedText(), QString()); } +void tst_qquicktextedit::keys_shortcutoverride() +{ + // Tests that QML TextEdit receives Keys.onShortcutOverride (QTBUG-68711) + QQuickView view; + view.setSource(testFileUrl("keys_shortcutoverride.qml")); + view.show(); + view.requestActivate(); + QVERIFY(QTest::qWaitForWindowActive(&view)); + QObject *root = view.rootObject(); + QVERIFY(root); + + QQuickTextEdit *textEdit = root->findChild<QQuickTextEdit*>(); + QVERIFY(textEdit); + QQuickRectangle *rectangle = root->findChild<QQuickRectangle*>(QLatin1String("rectangle")); + QVERIFY(rectangle); + + // Precondition: check if its not already changed + QCOMPARE(root->property("who").value<QString>(), QLatin1String("nobody")); + + // send Key_Escape to the Rectangle + QVERIFY(rectangle->hasActiveFocus()); + QTest::keyPress(&view, Qt::Key_Escape); + QCOMPARE(root->property("who").value<QString>(), QLatin1String("Rectangle")); + + // send Key_Escape to TextEdit + textEdit->setFocus(true); + QTest::keyPress(&view, Qt::Key_Escape); + QCOMPARE(root->property("who").value<QString>(), QLatin1String("TextEdit")); +} + QTEST_MAIN(tst_qquicktextedit) #include "tst_qquicktextedit.moc" |