Android: Set EnterKeyNext as default type for QLineEdit

Behavior of EnterKey for virtual keyboard need to be changed for
QLineEdit. Before this commit, ImeOption was set to IME_ACTION_DONE.
Because of that, setting any text in QLineEdit automatically accept
QDialogs. That was annoying, when more than one QLineEdit need to be
set.

[ChangeLog][Widgets][Android] EnterKey type is now changed from
EnterKeyDefault to EnterKeyNext for virtual keyboard in QLineEdit. It is
done only if the focus can be moved to widget below.

Fixes: QTBUG-61652
Change-Id: I98a7686f9f675fccf0112b8d27d48ad8fd7a887f
Reviewed-by: Assam Boudjelthia <assam.boudjelthia@qt.io>
Reviewed-by: Ville Voutilainen <ville.voutilainen@qt.io>
This commit is contained in:
Bartlomiej Moskal 2022-09-09 09:55:56 +02:00
parent 4b892b1034
commit 02f1e72d5a
2 changed files with 45 additions and 0 deletions

View File

@ -1802,6 +1802,22 @@ void QLineEdit::inputMethodEvent(QInputMethodEvent *e)
*/
QVariant QLineEdit::inputMethodQuery(Qt::InputMethodQuery property) const
{
#ifdef Q_OS_ANDROID
// QTBUG-61652
if (property == Qt::ImEnterKeyType) {
QWidget *next = nextInFocusChain();
while (next && next != this && next->focusPolicy() == Qt::NoFocus)
next = next->nextInFocusChain();
if (next) {
const auto nextYPos = next->mapToGlobal(QPoint(0, 0)).y();
const auto currentYPos = mapToGlobal(QPoint(0, 0)).y();
if (currentYPos < nextYPos)
// Set EnterKey to KeyNext type only if the next widget
// in the focus chain is below current QLineEdit
return Qt::EnterKeyNext;
}
}
#endif
return inputMethodQuery(property, QVariant());
}

View File

@ -267,6 +267,8 @@ private slots:
void inputMethodQueryImHints_data();
void inputMethodQueryImHints();
void inputMethodQueryEnterKeyType();
void inputMethodUpdate();
void undoRedoAndEchoModes_data();
@ -4409,6 +4411,33 @@ void tst_QLineEdit::inputMethodQueryImHints()
QCOMPARE(static_cast<Qt::InputMethodHints>(value.toInt()), hints);
}
void tst_QLineEdit::inputMethodQueryEnterKeyType()
{
QWidget mw;
QVBoxLayout layout(&mw);
QLineEdit le1(&mw);
layout.addWidget(&le1);
mw.show();
QVariant enterType = le1.inputMethodQuery(Qt::ImEnterKeyType);
QCOMPARE(enterType.value<Qt::EnterKeyType>(), Qt::EnterKeyDefault);
mw.hide();
QLineEdit le2(&mw);
layout.addWidget(&le2);
mw.show();
enterType = le1.inputMethodQuery(Qt::ImEnterKeyType);
#ifdef Q_OS_ANDROID
// QTBUG-61652
// EnterKey is changed to EnterKeyNext if the focus can be moved to widget below
QCOMPARE(enterType.value<Qt::EnterKeyType>(), Qt::EnterKeyNext);
#else
QCOMPARE(enterType.value<Qt::EnterKeyType>(), Qt::EnterKeyDefault);
#endif
enterType = le2.inputMethodQuery(Qt::ImEnterKeyType);
QCOMPARE(enterType.value<Qt::EnterKeyType>(), Qt::EnterKeyDefault);
}
void tst_QLineEdit::inputMethodUpdate()
{
if (QGuiApplication::platformName().startsWith(QLatin1String("wayland"), Qt::CaseInsensitive))