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 <richard.gustavsen@qt.io>
(cherry picked from commit 70096b2bbd54d35518167cb41ea3576b992c3cda)
Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
This commit is contained in:
Yansheng Zhu 2024-02-28 20:54:56 +08:00 committed by Qt Cherry-pick Bot
parent 58b9d558dd
commit 24f6eaa111
4 changed files with 6 additions and 5 deletions

View File

@ -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:

View File

@ -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);

View File

@ -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);

View File

@ -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