Qt开发中文输入法

http://hi.baidu.com/catproste2012/item/d7baa32d3f51ee0943634a18

首先感谢上文的博主,虽然数据库的方法没能正确显示中文,但是QSettings更有效的解决了这个问题;

     此过程,首先要找到py.txt类似的文件,然后读文件,可以分组保存每个拼音对应的汉字,也可以保存拼音对应的汉字串链表,本人采用的是后者,在读取后再用链表处理,这样读写相对方便快捷。

    注:py.txt文件不好找,我找了半天,先赚点积分,嘿嘿,1分~http://download.csdn.net/detail/styyzxjq2009/5021320

优点:

      1、键盘布局设计巧妙,比用layout更整齐、容易控制;

      2、QMapper的使用

      2、功能完整!

首先指出博主程序的不足:

    1、QSS样式不是很好,显示不是那么清楚

    2、没有焦点事件捕获

    3、没有关闭按钮


   参考API文档QWSInputMethod::updateHandler可以用来捕获输入焦点进入和失去焦点的事件,具体请看下面英文:

void QWSInputMethod::updateHandler ( int type ) [virtual]
Handles update events including resets and focus changes. The update events are specified by the given type which is one of the UpdateType enum values.

Note that reimplementations of this function must call the base implementation for all cases that it does not handle itself.


enum QWSInputMethod::UpdateType
This enum describes the various types of update events recognized by the input method.

Constant    Value    Description
QWSInputMethod::Update    0    The input widget is updated in some way; use sendQuery() with Qt::ImMicroFocus as an argument for more information.
QWSInputMethod::FocusIn    1    A new input widget receives focus.
QWSInputMethod::FocusOut    2    The input widget loses focus.
QWSInputMethod::Reset    3    The input method should be reset.
QWSInputMethod::Destroyed    4    The input widget is destroyed.



个人修改代码如下

void IPMethod::Escape()//添加退出按钮事件
{

    if(NULL==this->kb)
       return;
    else
        delete this->kb;
    this->kb=NULL;

}

void IPMethod::updateHandler(int type)
{    
    //QWidget *current_focus_widget;
    //        current_focus_widget = QApplication::focusWidget();
    //     le= qobject_cast(current_focus_widget);
    
        switch(type)
        {
        case QWSInputMethod::FocusIn:
               if(NULL==this->kb)// 创建并初始化键盘
               {
                  kb=new Keyboard();
    
                  mapper=new QSignalMapper(this);
                  connect(mapper,SIGNAL(mapped(QString)),this,SLOT(sendContent(QString)));

                  shift=num=chn=false;
                  pageCount=currentPage=0;

                 int i;
                for(i=0;i<128;i++)
                {
                   chinese[i]="";
                }

                 setMapping();
                setConnect();
        
        }
        moveToBottom();
         this->kb->show();
        
              
        
              // QMessageBox::information(0,"Focusin","Focusin");
               break;
        case QWSInputMethod::FocusOut:
                if(NULL==this->kb)
                    break;
                else  //删除键盘
            //le->setText(this->content);
            //this->content.clear();
                    delete this->kb;
                this->kb=NULL;
                //QMessageBox::information(0,"Focusout","Focusout");
                break;
        default:
                break;
        }
}

你可能感兴趣的:(Qt)