QInputMethod Class

QInputMethod 类提供了对活动文本输入法的访问。更多

Header: #include <QInputMethod>
CMake: find_package(Qt6 REQUIRED COMPONENTS Gui)
target_link_libraries(mytarget PRIVATE Qt6::Gui)
qmake: QT += gui
继承: QObject

公共类型

enum Action { Click, ContextMenu }

属性

公共功能

QRectF anchorRectangle() const
QRectF cursorRectangle() const
Qt::LayoutDirection inputDirection() const
QRectF inputItemClipRectangle() const
QRectF inputItemRectangle() const
QTransform inputItemTransform() const
bool isAnimating() const
bool isVisible() const
QRectF keyboardRectangle() const
QLocale locale() const
void setInputItemRectangle(const QRectF &rect)
void setInputItemTransform(const QTransform &transform)
void setVisible(bool visible)

公共插槽

void commit()
void hide()
void invokeAction(QInputMethod::Action a, int cursorPosition)
void reset()
void show()
void update(Qt::InputMethodQueries queries)

信号

静态公共成员

QVariant queryFocusObject(Qt::InputMethodQuery query, const QVariant &argument)

详细说明

QInputMethod 用于文本编辑器集成到平台文本输入法,更常用于应用程序视图查询各种文本输入法相关信息,如虚拟键盘可见性和键盘尺寸。

Qt Quick QInputMethod 还通过 作为 属性在 QML 中提供对 QInputMethod 的访问。Qt global object Qt.inputMethod

成员类型文档

enum QInputMethod::Action

表示用户执行的操作类型。

常数说明
QInputMethod::Click0普通点击/轻敲
QInputMethod::ContextMenu1上下文菜单点击/轻敲(例如右键或点击并按住)

另请参阅 invokeAction().

属性文档

[read-only] anchorRectangle : const QRectF

以窗口坐标表示的输入项锚点矩形。

锚点矩形通常被各种文本编辑控件(如文本预测弹出窗口)用于跟踪文本选择。

访问功能:

QRectF anchorRectangle() const

通知信号

void anchorRectangleChanged()

[read-only] animating : const bool

当虚拟键盘正在打开或关闭时为 true。

当键盘完全打开或关闭时,动画为假。当animatingtruevisibilitytrue 时,键盘处于打开状态。当animatingtruevisibility 为假时,键盘正在关闭。

访问功能:

bool isAnimating() const

通知信号:

void animatingChanged()

[read-only] cursorRectangle : const QRectF

以窗口坐标表示的输入项光标矩形。

光标矩形通常用于各种文本编辑控件,如文本预测弹出窗口,以跟踪输入的文本。

访问功能:

QRectF cursorRectangle() const

通知信号

void cursorRectangleChanged()

[read-only] inputDirection : const Qt::LayoutDirection

当前输入方向。

访问功能:

Qt::LayoutDirection inputDirection() const

提示信号:

void inputDirectionChanged(Qt::LayoutDirection newDirection)

[read-only] inputItemClipRectangle : const QRectF

以窗口坐标表示的输入项剪切矩形。

各种输入法(如虚拟键盘)通常使用裁剪后的输入矩形来确定有多少屏幕空间可供输入法使用。

访问功能:

QRectF inputItemClipRectangle() const

通知信号:

void inputItemClipRectangleChanged()

[read-only] keyboardRectangle : const QRectF

虚拟键盘在窗口坐标中的几何形状。

如果无法知道键盘的几何形状,则可能是一个空矩形。安卓系统中的浮动键盘就是这种情况。

访问功能:

QRectF keyboardRectangle() const

通知信号:

void keyboardRectangleChanged()

[read-only] locale : const QLocale

当前输入语言。

访问功能:

QLocale locale() const

提示信号:

void localeChanged()

[read-only] visible : const bool

虚拟键盘在屏幕上的可见性

对于没有虚拟键盘的设备,输入法可见性仍为假。

访问功能:

bool isVisible() const

通知信号:

void visibleChanged()

另请参阅 show() 和hide()。

成员函数文档

[slot] void QInputMethod::commit()

将用户正在输入的单词提交到编辑器。具有文本预测功能的输入法以及输入字符时使用的脚本与实际添加到编辑器中的脚本不同的输入法大多需要该函数。任何中断文字合成的操作都需要通过调用 commit() 函数来清除合成状态,例如当光标移动到其他地方时。

[slot] void QInputMethod::hide()

请求关闭虚拟键盘。

通常情况下,应用程序不需要调用此函数,因为当文本编辑器失去焦点时(例如父视图关闭时),键盘应自动关闭。

QRectF QInputMethod::inputItemRectangle() const

以输入项坐标为单位返回输入项的几何图形。

另请参阅 setInputItemRectangle()。

QTransform QInputMethod::inputItemTransform() const

返回从输入项目坐标到窗口坐标的变换。

另请参阅 setInputItemTransform()。

[slot] void QInputMethod::invokeAction(QInputMethod::Action a, int cursorPosition)

当用户点击当前正在组成的单词(如操作a 和给定的cursorPosition 所示)时,由输入项调用。输入法通常会利用这一信息向用户提供更多的单词建议。

[static] QVariant QInputMethod::queryFocusObject(Qt::InputMethodQuery query, const QVariant &argument)

向当前焦点对象发送query ,参数argument ,并返回结果。

[slot] void QInputMethod::reset()

重置输入法状态。例如,文本编辑器通常会在插入文本前调用此方法,使部件准备好接受文本。

当聚焦的编辑器发生变化时,输入法会自动重置。

void QInputMethod::setInputItemRectangle(const QRectF &rect)

以输入项坐标设置输入项的几何图形为rect 。每当项目在场景中移动或焦点改变时,焦点窗口(如 QQuickCanvas)都需要更新该几何图形。

另请参阅 inputItemRectangle().

void QInputMethod::setInputItemTransform(const QTransform &transform)

将输入项目坐标到窗口坐标的变换设置为transform 。每当项目在场景中移动时,项目变换需要像 QQuickCanvas 一样由聚焦窗口更新。

另请参阅 inputItemTransform() 。

void QInputMethod::setVisible(bool visible)

控制键盘的可见性。相当于调用show() (如果visibletrue )或hide() (如果visiblefalse )。

另请参阅 isVisible()、show() 和hide()。

[slot] void QInputMethod::show()

请求打开虚拟键盘。如果平台不提供虚拟键盘,则可见性仍为 false。

通常情况下,应用程序不需要调用此函数,键盘应在文本编辑器获得焦点时自动打开。

[slot] void QInputMethod::update(Qt::InputMethodQueries queries)

当编辑器的输入法查询属性发生状态变化时,由输入项调用以通知平台输入法。调用该函数时,必须使用queries 参数来说明发生了哪些变化,输入法可以使用QInputMethodQueryEvent 对其感兴趣的属性进行查询。

尤其是在光标位置发生变化时调用 update 非常重要,因为这通常会导致其他查询属性(如周围文本和文本选择)也发生变化。为方便起见,经常与光标位置一起变化的属性已在Qt::ImQueryInput 值中分组。

© 2025 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.