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


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(软键盘)等功能。

下面是实现的代码:

  1. #include "inputmethod.h"   
  2. #include "widget.h"   
  3. InputMethod::InputMethod()  
  4. {  
  5.     keyMap = new KeyMap(this);  
  6.  keyMap->hide();  
  7. }  
  8. InputMethod::~InputMethod()  
  9. {  
  10.   
  11. }  
  12. void InputMethod::sendPreString(const QString &newString)  
  13. {  
  14.     if(newString == "Del")  
  15.     {  
  16.         inputString.resize(inputString.length()-1);  
  17.         this->sendPreeditString(inputString,0);  
  18.     }  
  19.     else  
  20.     {  
  21.         inputString += newString;  
  22.         this->sendPreeditString(inputString,0);  
  23.     }  
  24. }  
  25. void InputMethod::confirmString()  
  26. {  
  27.     this->sendCommitString(inputString);  
  28.     inputString.clear();  
  29. }  
  30. void InputMethod::updateHandler(int type)  
  31. {  
  32.  switch(type)  
  33.  {  
  34.  case QWSInputMethod::FocusOut:  
  35.   inputString.clear();  
  36.   //keyMap->hide();   
  37.  case QWSInputMethod::FocusIn:  
  38.   keyMap->show();  
  39.  default:  
  40.   break;  
  41.  }  
  42. }  

相关内容