Android: Fix handling of cursor position when stop composing

This is a workaround for a problem in TextEdit.
The symptom is that when the user places the cursor inside
of a word and hits backspace, the last letter of the word
is removed instead of the letter just before the cursor.
The reason is as follows.
When stopping composing, the current cursor position has to
be maintained. To that end, QAndroidInputContext sends an
event containing the text to be committed and the cursor position
to the editor. But the resulting cursor position is wrong.
This patch adapts QAndroidInputContext to send two events:
One to commit the text, the second to place the cursor.
A real fix would fix the editor to correctly
handle the event containing both the committed text and
the cursor position.

Fixes: QTBUG-97491
Pick-to: 6.2 5.15
Change-Id: Idd00e5afcbfe29c9cb77356f9add2e881c51b9bb
Reviewed-by: Paul Olav Tvete <paul.tvete@qt.io>
bb10
Andreas Buhr 2021-10-14 05:20:23 +02:00
parent 2e73ff1079
commit a47f66cee2
1 changed files with 15 additions and 7 deletions

View File

@ -1225,13 +1225,21 @@ bool QAndroidInputContext::focusObjectStopComposing()
m_composingCursor = -1;
// Moving Qt's cursor to where the preedit cursor used to be
QList<QInputMethodEvent::Attribute> attributes;
attributes.append(QInputMethodEvent::Attribute(QInputMethodEvent::Selection, localCursorPos, 0));
QInputMethodEvent event(QString(), attributes);
event.setCommitString(m_composingText);
sendInputMethodEvent(&event);
{
// commit the composing test
QList<QInputMethodEvent::Attribute> attributes;
QInputMethodEvent event(QString(), attributes);
event.setCommitString(m_composingText);
sendInputMethodEvent(&event);
}
{
// Moving Qt's cursor to where the preedit cursor used to be
QList<QInputMethodEvent::Attribute> attributes;
attributes.append(
QInputMethodEvent::Attribute(QInputMethodEvent::Selection, localCursorPos, 0));
QInputMethodEvent event(QString(), attributes);
sendInputMethodEvent(&event);
}
return true;
}