基于嵌入式QTE的输入法基本方法

来源:互联网 发布:数据库怎么导入excel 编辑:程序博客网 时间:2024/06/05 15:23
 

QtE的输入法框架必须提供一个QWSInputMethod类的实例, 所以在输入法中要实现一个QWSInputMethod类的派生类,即子类QWSInputMethod *input; 在此派生类中显示和操作软键盘widget并完成与输入法框架的通讯。 QWSServer进程调用(即你的主窗体)QWSServer::setCurrentInputMethod(QWSInputMethod*)激活该输入法后, 输入法框架会把应用程序的信息发送给QWSInputMethod类, 由QWSInputMethod的虚函数来处理。 所以最核心的部分变成怎样去实现一个QWSInputMethod的派生类,另外怎么让你的软键盘窗口能和程序输入窗口和QWSInputMethod和平共存,可以在软键盘类中的构造函数加入QWidget(0 , Qt::Tool | Qt::WindowStaysOnTopHint),软键盘中有过个按钮可以利用QSignMapper类的派生类,用若干个按钮连接一个槽,来执行相应的输入(这里是QSignMapper的基本用法)。  QWSInputMethod提供的sendPreeditString方法负责将预处理的文本发送给焦点窗体, 一般情况下编辑器会将此文本显示为带下划线或虚线的文本, 表示这是编辑过程中的半成品; 然后QWSInputMethod::sendCommitString函数负责发送最终用户确认的文本给编辑框。

QWSInputMethod派生类还应该去实现updateHandler虚函数,该虚函数是输入法框架和输入法之间的桥梁, 专门向输入法发送一些状态信息, 比如在焦点离开或进入编辑框时updateHandler函数就会被调用到, 在这里加入你的输入法的处理可以实现在适当时机显示或隐藏输入法widget(软键盘)等功能。

下面是实现的代码:

 

#include "inputmethod.h"#include "widget.h"InputMethod::InputMethod(){    keyMap = new KeyMap(this); keyMap->hide();}InputMethod::~InputMethod(){}void InputMethod::sendPreString(const QString &newString){    if(newString == "Del")    {        inputString.resize(inputString.length()-1);        this->sendPreeditString(inputString,0);    }    else    {        inputString += newString;        this->sendPreeditString(inputString,0);    }}void InputMethod::confirmString(){    this->sendCommitString(inputString);    inputString.clear();}void InputMethod::updateHandler(int type){ switch(type) { case QWSInputMethod::FocusOut:  inputString.clear();  //keyMap->hide(); case QWSInputMethod::FocusIn:  keyMap->show(); default:  break; }}

原创粉丝点击