diff --git a/src/editor/dtextedit.cpp b/src/editor/dtextedit.cpp index ed537ebb..13c26e3c 100644 --- a/src/editor/dtextedit.cpp +++ b/src/editor/dtextedit.cpp @@ -6630,43 +6630,76 @@ void TextEdit::inputMethodEvent(QInputMethodEvent *e) return; } - if (m_isPreeditBefore) { - // 每次 preedit 都是完整的字符串,所以撤销上次的 preedit - undo_(); - - // 将光标移回原位 - if (Overwrite == m_cursorMode) { - auto cursor = textCursor(); - cursor.movePosition(QTextCursor::PreviousCharacter, QTextCursor::MoveAnchor, m_preeditLengthBefore); - setTextCursor(cursor); - } - } - bool isPreedit = !e->preeditString().isEmpty(); - if (isPreedit || !e->commitString().isEmpty()) { - const QString &text = isPreedit ? e->preeditString() : e->commitString(); + // 标记进入 preedit 更新过程,屏蔽中间态信号,避免输入法查询到撤销后的旧坐标 + m_bInputMethodUpdating = true; + // 进入前先缓存当前(正确的)光标矩形,供 inputMethodQuery 在屏蔽期间返回 + m_cachedCursorRect = QPlainTextEdit::cursorRect(); - //列编辑添加撤销重做 - if (m_bIsAltMod && !m_altModSelections.isEmpty()) { - insertColumnEditTextEx(text); - } else { - // 覆盖模式下输入法输入时,单独处理,模拟选中替换处理 + // 用单独作用域包裹 QSignalBlocker,确保 updateMicroFocus 在解除屏蔽后调用 + { + const QSignalBlocker blocker(this); + + if (m_isPreeditBefore) { + // 每次 preedit 都是完整的字符串,所以撤销上次的 preedit + undo_(); + + // 将光标移回原位 if (Overwrite == m_cursorMode) { - auto cursor = this->textCursor(); - // 设置光标选中后续与当前输入法输入的文本相同长度 - cursor.movePosition(QTextCursor::NextCharacter, QTextCursor::KeepAnchor, text.size()); - // 使用此光标信息插入将输入的文本替换选中的长度 - insertSelectTextEx(cursor, text); + auto cursor = textCursor(); + cursor.movePosition(QTextCursor::PreviousCharacter, QTextCursor::MoveAnchor, m_preeditLengthBefore); + setTextCursor(cursor); + } + } + bool isPreedit = !e->preeditString().isEmpty(); + if (isPreedit || !e->commitString().isEmpty()) { + const QString &text = isPreedit ? e->preeditString() : e->commitString(); + + //列编辑添加撤销重做 + if (m_bIsAltMod && !m_altModSelections.isEmpty()) { + insertColumnEditTextEx(text); } else { - insertSelectTextEx(textCursor(), text); + // 覆盖模式下输入法输入时,单独处理,模拟选中替换处理 + if (Overwrite == m_cursorMode) { + auto cursor = this->textCursor(); + // 设置光标选中后续与当前输入法输入的文本相同长度 + cursor.movePosition(QTextCursor::NextCharacter, QTextCursor::KeepAnchor, text.size()); + // 使用此光标信息插入将输入的文本替换选中的长度 + insertSelectTextEx(cursor, text); + } else { + insertSelectTextEx(textCursor(), text); + } } + + m_isSelectAll = false; } - m_isSelectAll = false; + m_isPreeditBefore = isPreedit; + m_preeditLengthBefore = e->preeditString().length(); + + // 更新缓存为最终光标矩形,解除屏蔽后对外返回的是正确坐标 + m_cachedCursorRect = QPlainTextEdit::cursorRect(); } + // blocker 在此析构,解除信号屏蔽 + + // 关闭 query 屏蔽标志,后续输入法查询走默认实现拿到正确坐标 + m_bInputMethodUpdating = false; + // 主动触发一次 microFocus 更新,让输入法拿到最终坐标 + updateMicroFocus(); +} - m_isPreeditBefore = isPreedit; - m_preeditLengthBefore = e->preeditString().length(); +QVariant TextEdit::inputMethodQuery(Qt::InputMethodQuery query) const +{ + // 在 preedit 更新过程(undo 旧 preedit + 重新插入新 preedit)中, + // 光标会短暂处于撤销后的旧位置。此时若输入法(如搜狗候选首选项) + // 查询光标矩形,会拿到错误的旧坐标导致候选框左跳。 + // 屏蔽期间统一返回缓存的正确坐标,避免中间态坐标外泄。 + // 注意:ImMicroFocus 是 ImCursorRectangle 的废弃别名,两者值相同(0x2), + // 故只需判断 ImCursorRectangle 即可同时覆盖两者。 + if (m_bInputMethodUpdating && query == Qt::ImCursorRectangle) { + return QVariant(m_cachedCursorRect); + } + return QPlainTextEdit::inputMethodQuery(query); } void TextEdit::mousePressEvent(QMouseEvent *e) diff --git a/src/editor/dtextedit.h b/src/editor/dtextedit.h index a15c1844..2ad2a66d 100644 --- a/src/editor/dtextedit.h +++ b/src/editor/dtextedit.h @@ -578,6 +578,7 @@ public slots: void dragMoveEvent(QDragMoveEvent *event) override; void dropEvent(QDropEvent *event) override; void inputMethodEvent(QInputMethodEvent *e) override; + QVariant inputMethodQuery(Qt::InputMethodQuery query) const override; void mousePressEvent(QMouseEvent *e) override; void mouseMoveEvent(QMouseEvent *e) override; void mouseReleaseEvent(QMouseEvent *e) override; @@ -889,6 +890,11 @@ private slots: bool m_isPreeditBefore = false; // 上一个输入法时间是否是 preedit int m_preeditLengthBefore = 0; + // preedit 更新过程中(undo 旧 preedit + 重新插入新 preedit)屏蔽中间态信号, + // 避免输入法查询到撤销后的旧坐标导致候选框左跳(搜狗候选首选项场景) + bool m_bInputMethodUpdating = false; + mutable QRect m_cachedCursorRect; // 屏蔽期间对外返回的缓存光标矩形 + Qt::CaseSensitivity defaultCaseSensitive = Qt::CaseInsensitive; // 查找匹配时默认不区分 }; #endif