QInputMethodEvent 类

QInputMethodEvent class provides parameters for input method events. 更多...

头: #include <QInputMethodEvent>
qmake: QT += gui
继承: QEvent

公共类型

class 属性
enum AttributeType { TextFormat, Cursor, Language, Ruby, Selection }

公共函数

QInputMethodEvent ()
QInputMethodEvent (const QString & preeditText , const QList<Attribute> & attributes )
QInputMethodEvent (const QInputMethodEvent & other )
~QInputMethodEvent ()
const QList<Attribute> & attributes () const
const QString & commitString () const
const QString & preeditString () const
int replacementLength () const
int replacementStart () const
void setCommitString (const QString & commitString , int replaceFrom = 0, int replaceLength = 0)

额外继承成员

详细描述

QInputMethodEvent class provides parameters for input method events.

输入法事件被发送给 Widget,当使用输入法把文本键入 Widget 时。输入法被广泛用于键入非拉丁字母语言的文本。

注意:当创建自定义文本编辑 Widget 时, Qt::WA_InputMethodEnabled 窗口属性必须被明确设置 (使用 QWidget::setAttribute () 函数) 为接收输入法事件。

希望能够正确处理具有复杂字符输入的语言的键盘键入 Widget 的作者,对这些事件很感兴趣。这种语言的文本输入过程通常分 3 个步骤:

  1. 开始合成

    当用户在键盘上按下首个键时,输入上下文被创建。此输入上下文将包含已键入字符的字符串。

  2. 正在合成

    当每按下一个新键,输入法会试着为迄今为止键入的文本创建匹配字符串 (又称预编辑字符串)。当输入上下文处于活动状态时,用户只可以把光标移入属于此输入上下文的字符串中。

  3. 完成

    在某个时间点,用户会激活用户界面组件 (或许使用特定键),在那里用户可以从大量匹配迄今为止键入文本的字符串中进行选择。用户可以确认其选择或取消输入;无论哪种情况,输入上下文都会被关闭。

QInputMethodEvent models these three stages, and transfers the information needed to correctly render the intermediate result. A QInputMethodEvent has two main parameters: preeditString () 和 commitString ()。 preeditString () 参数给出目前活动的预编辑字符串。 commitString () 参数给出应该被添加到编辑器 Widget 文本中 (或部分替换) 的文本。它通常是输入操作的结果,且必须在预编辑字符串之前直接将其插入 Widget 文本中。

commitString () 应替换部分编辑器文本, replacementLength () 将包含要替换的字符数。 replacementStart () 包含相对于预编辑字符串开头的要替换字符的位置。

许多属性控制预编辑字符串的视觉外观 (预编辑字符串之外的文本的视觉外观仅由 Widget 控制)。 AttributeType 枚举描述了可以设置的不同属性。

类实现 QWidget::inputMethodEvent () 或 QGraphicsItem::inputMethodEvent () 至少应该理解和尊重 TextFormat and Cursor 属性。

由于输入法需要能够从 Widget (或图形项) 查询某些特性,因此子类也必须实现 QWidget::inputMethodQuery () 和 QGraphicsItem::inputMethodQuery () 分别。

当收到输入法事件时,文本 Widget 必须履行以下步骤:

  1. 若 Widget 有选择文本,就应该移除所选文本。
  2. 移除文本始于 replacementStart () 采用长度 replacementLength () 并替换它通过 commitString ()。若 replacementLength () 为 0, replacementStart () 给出插入位置为 commitString ().

    置换时预编辑字符串区域被忽略,因此从 -1 开始长度为 2 的置换将移除预编辑字符串之前的最后一个字符和之后的第一个字符,并将提交字符串直接插入预编辑字符串之前。

    若 Widget 有实现撤销/重做,此操作将被添加到撤销堆栈。

  3. 若当前没有预编辑字符串,插入 preeditString () 在当前光标位置;否则替换先前 preeditString 采用来自此事件的某一接收。

    若 Widget 有实现撤销/重做, preeditString () 不应该以任何方式影响撤消/重做堆栈。

    Widget 应审查要应用于预编辑字符串的属性列表。它必须至少理解 TextFormat 和 Cursor 属性及按指定渲染它们。

另请参阅 QInputMethod .

成员类型文档编制

enum QInputMethodEvent:: AttributeType

常量 描述
QInputMethodEvent::TextFormat 0 A QTextCharFormat for the part of the preedit string specified by start and length. value contains a QVariant 类型 QTextFormat specifying rendering of this part of the preedit string. There should be at most one format for every part of the preedit string. If several are specified for any character in the string the behaviour is undefined. A conforming implementation has to at least honor the backgroundColor, textColor and fontUnderline properties of the format.
QInputMethodEvent::Cursor 1 If set, a cursor should be shown inside the preedit string at position start. The length variable determines whether the cursor is visible or not. If the length is 0 the cursor is invisible. If value is a QVariant 类型 QColor this color will be used for rendering the cursor, otherwise the color of the surrounding text will be used. There should be at most one Cursor attribute per event. If several are specified the behaviour is undefined.
QInputMethodEvent::Language 2 The variant contains a QLocale object specifying the language of a certain part of the preedit string. There should be at most one language set for every part of the preedit string. If several are specified for any character in the string the behavior is undefined.
QInputMethodEvent::Ruby 3 The ruby text for a part of the preedit string. There should be at most one ruby text set for every part of the preedit string. If several are specified for any character in the string the behaviour is undefined.
QInputMethodEvent::Selection 4 If set, the edit cursor should be moved to the specified position in the editor text contents. In contrast with Cursor , this attribute does not work on the preedit text, but on the surrounding text. The cursor will be moved after the commit string has been committed, and the preedit string will be located at the new edit position. The start position specifies the new position and the length variable can be used to set a selection starting from that point. The value is unused.

另请参阅 属性 .

成员函数文档编制

QInputMethodEvent:: QInputMethodEvent ()

构造事件为类型 QEvent::InputMethod attributes (), preeditString (), commitString (), replacementStart (),和 replacementLength () 被初始化为默认值。

另请参阅 setCommitString ().

QInputMethodEvent:: QInputMethodEvent (const QString & preeditText , const QList < 属性 > & attributes )

构造事件为类型 QEvent::InputMethod 。预编辑文本被设为 preeditText ,属性为 attributes .

commitString (), replacementStart (),和 replacementLength () 值可以设置使用 setCommitString ().

另请参阅 preeditString () 和 attributes ().

QInputMethodEvent:: QInputMethodEvent (const QInputMethodEvent & other )

构造副本为 other .

QInputMethodEvent:: ~QInputMethodEvent ()

Destroys the instance of QInputMethodEvent.

const QList < 属性 > &QInputMethodEvent:: attributes () const

Returns the list of attributes passed to the QInputMethodEvent constructor. The attributes control the visual appearance of the preedit string (the visual appearance of text outside the preedit string is controlled by the widget only).

另请参阅 preeditString () 和 属性 .

const QString &QInputMethodEvent:: commitString () const

Returns the text that should get added to (or replace parts of) the text of the editor widget. It usually is a result of the input operations and has to be inserted to the widgets text directly before the preedit string.

另请参阅 setCommitString (), preeditString (), replacementStart (),和 replacementLength ().

const QString &QInputMethodEvent:: preeditString () const

返回预编辑文本,即:用户开始编辑之前的文本。

另请参阅 commitString () 和 attributes ().

int QInputMethodEvent:: replacementLength () const

返回预编辑字符串中要被替换的字符数。

另请参阅 replacementStart () 和 setCommitString ().

int QInputMethodEvent:: replacementStart () const

返回要替换字符的位置,相对于预编辑字符串开头。

另请参阅 replacementLength () 和 setCommitString ().

void QInputMethodEvent:: setCommitString (const QString & commitString , int replaceFrom = 0, int replaceLength = 0)

把提交字符串设为 commitString .

The commit string is the text that should get added to (or replace parts of) the text of the editor widget. It usually is a result of the input operations and has to be inserted to the widgets text directly before the preedit string.

If the commit string should replace parts of the of the text in the editor, replaceLength specifies the number of characters to be replaced. replaceFrom specifies the position at which characters are to be replaced relative from the start of the preedit string.

另请参阅 commitString (), replacementStart (),和 replacementLength ().