本页内容

QInputMethod Class

QInputMethod 类用于访问当前活动的文本输入法。更多内容...

头文件: #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 此外,还通过Qt global object 将Qt.inputMethod 属性引入QML,以便访问QInputMethod。

成员类型文档

enum QInputMethod::Action

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

常量值描述
QInputMethod::Click0普通点击/轻触
QInputMethod::ContextMenu1上下文菜单点击/轻触(例如:右键或长按)

另请参阅 ` invokeAction()`。

属性文档

[read-only] anchorRectangle : QRectF

输入项目在窗口坐标系中的锚点矩形。

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

访问函数:

QRectF anchorRectangle() const

通知信号:

void anchorRectangleChanged()

[read-only] animating : bool

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

当键盘完全展开或收起时,动画状态为false。当animating 为true 且visibility 为true 时,键盘正在展开;当animating 为true 且visibility 为false时,键盘正在收起。

访问函数:

bool isAnimating() const

通知器信号:

void animatingChanged()

[read-only] cursorRectangle : QRectF

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

光标矩形常被各种文本编辑控件(如文本预测弹出窗口)用于跟踪正在输入的文本。

访问函数:

QRectF cursorRectangle() const

通知信号:

void cursorRectangleChanged()

[read-only] inputDirection : Qt::LayoutDirection

当前输入方向。

访问功能:

Qt::LayoutDirection inputDirection() const

通知信号:

void inputDirectionChanged(Qt::LayoutDirection newDirection)

[read-only] inputItemClipRectangle : QRectF

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

各种输入法通常会使用该裁剪后的输入矩形来确定可供该输入法使用的屏幕区域大小(例如虚拟键盘)。

访问函数:

QRectF inputItemClipRectangle() const

通知信号:

void inputItemClipRectangleChanged()

[read-only] keyboardRectangle : QRectF

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

如果无法确定键盘的几何尺寸,此处可能是一个空矩形。Android 系统上的浮动键盘即属于这种情况。

访问函数:

QRectF keyboardRectangle() const

通知器信号:

void keyboardRectangleChanged()

[read-only] locale : QLocale

当前输入区域设置。

访问函数:

QLocale locale() const

通知器信号:

void localeChanged()

[read-only] visible : bool

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

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

访问函数:

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()(如果visible 的值为true )或hide()(如果visible 的值为false )。

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

[slot] void QInputMethod::show()

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

通常情况下,应用程序无需调用此函数,当文本编辑器获得焦点时,键盘应自动弹出。

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

由输入项调用,用于在编辑器的输入法查询属性发生状态变化时通知平台输入法。调用函数queries 时,必须使用该参数来指示发生了哪些变化,输入法可利用该参数通过QInputMethodQueryEvent 查询其感兴趣的属性。

特别需要注意的是,每当光标位置发生变化时,调用 update 函数非常重要,因为这通常会导致其他查询属性(如周围文本和文本选区)也随之变化。为了方便起见,那些通常会随光标位置一同变化的属性已被归类到Qt::ImQueryInput 的值中。

© 2026 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.