From 24f6eaa111e2a51ffd32f09d0a76b20acc659643 Mon Sep 17 00:00:00 2001 From: Yansheng Zhu <670429759@qq.com> Date: Wed, 28 Feb 2024 20:54:56 +0800 Subject: [PATCH] Implement Qt::ImEnabled by isEnabled() and isReadOnly() Previously, querying Qt::ImEnabled only returned the value of isEnabling(), which is incorrect for edit widgets with read-only properties set, as Qt::ImEnabled indicates whether text can be *input* through the input method, which results in the IM being able to insert text into read-only edit widgets. The fixed version uses both isEnabling() and isReadOnly() values to determine whether input methods need to be enabled. For some platforms (like iOS and Android) that rely on IM to select text, a check for ImReadOnly has been added to their QPA plugins to enable handles on read-only input boxes. At the same time, the imEnabledNotImplemented function in the test file tst_qwidget was modified, since ImEnabling should give a _false_ value when a lineedit is read-only. Task-number: QTBUG-105009 Task-number: QTBUG-110838 Task-number: QTBUG-119182 Pick-to: 6.6 6.5 Change-Id: Ia2abcdb3200826d567f90447d4f8b71d0ef1fbf0 Reviewed-by: Yansheng Zhu <670429759@qq.com> Reviewed-by: Richard Moe Gustavsen (cherry picked from commit 70096b2bbd54d35518167cb41ea3576b992c3cda) Reviewed-by: Qt Cherry-pick Bot --- src/widgets/widgets/qlineedit.cpp | 2 +- src/widgets/widgets/qplaintextedit.cpp | 2 +- src/widgets/widgets/qtextedit.cpp | 2 +- tests/auto/widgets/kernel/qwidget/tst_qwidget.cpp | 5 +++-- 4 files changed, 6 insertions(+), 5 deletions(-) diff --git a/src/widgets/widgets/qlineedit.cpp b/src/widgets/widgets/qlineedit.cpp index 201ecd2b69d..8909ac80d9b 100644 --- a/src/widgets/widgets/qlineedit.cpp +++ b/src/widgets/widgets/qlineedit.cpp @@ -1845,7 +1845,7 @@ QVariant QLineEdit::inputMethodQuery(Qt::InputMethodQuery property, QVariant arg Q_D(const QLineEdit); switch(property) { case Qt::ImEnabled: - return isEnabled(); + return isEnabled() && !isReadOnly(); case Qt::ImCursorRectangle: return d->cursorRect(); case Qt::ImAnchorRectangle: diff --git a/src/widgets/widgets/qplaintextedit.cpp b/src/widgets/widgets/qplaintextedit.cpp index dcba223cb03..305d5a0e056 100644 --- a/src/widgets/widgets/qplaintextedit.cpp +++ b/src/widgets/widgets/qplaintextedit.cpp @@ -2208,7 +2208,7 @@ QVariant QPlainTextEdit::inputMethodQuery(Qt::InputMethodQuery query, QVariant a Q_D(const QPlainTextEdit); switch (query) { case Qt::ImEnabled: - return isEnabled(); + return isEnabled() && !isReadOnly(); case Qt::ImHints: case Qt::ImInputItemClipRectangle: return QWidget::inputMethodQuery(query); diff --git a/src/widgets/widgets/qtextedit.cpp b/src/widgets/widgets/qtextedit.cpp index 1a3d0f9e983..c749bed593c 100644 --- a/src/widgets/widgets/qtextedit.cpp +++ b/src/widgets/widgets/qtextedit.cpp @@ -1816,7 +1816,7 @@ QVariant QTextEdit::inputMethodQuery(Qt::InputMethodQuery query, QVariant argume Q_D(const QTextEdit); switch (query) { case Qt::ImEnabled: - return isEnabled(); + return isEnabled() && !isReadOnly(); case Qt::ImHints: case Qt::ImInputItemClipRectangle: return QWidget::inputMethodQuery(query); diff --git a/tests/auto/widgets/kernel/qwidget/tst_qwidget.cpp b/tests/auto/widgets/kernel/qwidget/tst_qwidget.cpp index f7a98e1ef6d..761d341279b 100644 --- a/tests/auto/widgets/kernel/qwidget/tst_qwidget.cpp +++ b/tests/auto/widgets/kernel/qwidget/tst_qwidget.cpp @@ -11664,11 +11664,12 @@ void tst_QWidget::imEnabledNotImplemented() QVERIFY(imEnabled.isValid()); QVERIFY(imEnabled.toBool()); - // ...even if it's read-only + // ImEnabled should be false when a lineedit is read-only since + // ImEnabled indicates the widget accepts input method _input_. edit.setReadOnly(true); imEnabled = QApplication::inputMethod()->queryFocusObject(Qt::ImEnabled, QVariant()); QVERIFY(imEnabled.isValid()); - QVERIFY(imEnabled.toBool()); + QVERIFY(!imEnabled.toBool()); } #ifdef QT_BUILD_INTERNAL