From b9b26b2b31489fa54a23a29ed419a3d7a611667a Mon Sep 17 00:00:00 2001 From: Axel Spoerl Date: Sun, 13 Aug 2023 11:39:01 +0200 Subject: QAndroidPlatformInputContext: send composition text and cursor jointly MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit QAndroidPlatformInputContext::focusObjectStopComposing() sends an input event for each character newly added by the Android virtual keyboard. It then sends a second input event to notify that the cursor has advanced to the position after the new character. The implicit assumption is, that the receiver of the input event does not change the text. If e.g. QLineEdit::setText() is called in the QLineEdit::textEdited slot, the text does change. If the change implies a cursor change, QLineEdit notifies the platform input context about it. However, by sending the second input event, QAndroidPlatformContent returns the cursor back to the position after the last character added by the virtual keyboard. This patch joins the composed text and the cursor position into one single input method event. A new cursor position, set by the receiver of the input method event, is no longer overridden. The patch adds test functionality to tst_QLineEdit::setText(). Fixes: QTBUG-115756 Change-Id: I85ffac5d6bab93ccb144be0f5b8083258a270550 Reviewed-by: Tor Arne Vestbø (cherry picked from commit be3b9b2ab12f664c196d649e8c4247d70805d667) Reviewed-by: Qt Cherry-pick Bot --- .../platforms/android/qandroidinputcontext.cpp | 22 ++++-------- .../widgets/widgets/qlineedit/tst_qlineedit.cpp | 40 +++++++++++++++++++--- 2 files changed, 42 insertions(+), 20 deletions(-) diff --git a/src/plugins/platforms/android/qandroidinputcontext.cpp b/src/plugins/platforms/android/qandroidinputcontext.cpp index 521d3086b57..5d539d7da43 100644 --- a/src/plugins/platforms/android/qandroidinputcontext.cpp +++ b/src/plugins/platforms/android/qandroidinputcontext.cpp @@ -1131,21 +1131,13 @@ bool QAndroidInputContext::focusObjectStopComposing() m_composingCursor = -1; - { - // commit the composing test - QList attributes; - QInputMethodEvent event(QString(), attributes); - event.setCommitString(m_composingText); - sendInputMethodEvent(&event); - } - { - // Moving Qt's cursor to where the preedit cursor used to be - QList attributes; - attributes.append( - QInputMethodEvent::Attribute(QInputMethodEvent::Selection, localCursorPos, 0)); - QInputMethodEvent event(QString(), attributes); - sendInputMethodEvent(&event); - } + // commit composing text and cursor position + QList attributes; + attributes.append( + QInputMethodEvent::Attribute(QInputMethodEvent::Selection, localCursorPos, 0)); + QInputMethodEvent event(QString(), attributes); + event.setCommitString(m_composingText); + sendInputMethodEvent(&event); return true; } diff --git a/tests/auto/widgets/widgets/qlineedit/tst_qlineedit.cpp b/tests/auto/widgets/widgets/qlineedit/tst_qlineedit.cpp index 94e2a2fa243..ac3fd82353c 100644 --- a/tests/auto/widgets/widgets/qlineedit/tst_qlineedit.cpp +++ b/tests/auto/widgets/widgets/qlineedit/tst_qlineedit.cpp @@ -1588,14 +1588,44 @@ void tst_QLineEdit::textMask() QCOMPARE( testWidget->text(), insertString ); } +class LineEditChangingText : public QLineEdit +{ + Q_OBJECT + +public: + LineEditChangingText(QWidget *parent) : QLineEdit(parent) + { + connect(this, &QLineEdit::textEdited, this, &LineEditChangingText::onTextEdited); + } + +public slots: + void onTextEdited(const QString &text) + { + if (text.length() == 3) + setText(text + "-"); + } +}; + void tst_QLineEdit::setText() { QLineEdit *testWidget = ensureTestWidget(); - QSignalSpy editedSpy(testWidget, SIGNAL(textEdited(QString))); - QSignalSpy changedSpy(testWidget, SIGNAL(textChanged(QString))); - testWidget->setText("hello"); - QCOMPARE(editedSpy.size(), 0); - QCOMPARE(changedSpy.value(0).value(0).toString(), QString("hello")); + { + QSignalSpy editedSpy(testWidget, &QLineEdit::textEdited); + QSignalSpy changedSpy(testWidget, &QLineEdit::textChanged); + testWidget->setText("hello"); + QCOMPARE(editedSpy.size(), 0); + QCOMPARE(changedSpy.value(0).value(0).toString(), QString("hello")); + } + + QTestEventList keys; + keys.addKeyClick(Qt::Key_A); + keys.addKeyClick(Qt::Key_B); + keys.addKeyClick(Qt::Key_C); + + LineEditChangingText lineEdit(nullptr); + keys.simulate(&lineEdit); + QCOMPARE(lineEdit.text(), "abc-"); + QCOMPARE(lineEdit.cursorPosition(), 4); } void tst_QLineEdit::displayText_data() -- cgit v1.2.3