Qt输入法设计(嵌入式)

【更新】

2012-7-9,设计模式(5)-装饰模式(Decorator),运用装饰模式改进的版本

2012-6-6,设计模式(1)-模板模式,运用模板模式改进的版本

2012-5-29,添加源码示例下载地址(文章末尾)

【描述】

用QT制作了输入法,能输入英文和数字以及字符。简要介绍,示出关键代码。在QLineEdit控件和QTableWidget中均可以使用,本例只给出了QLineEdit的情况。

【效果】

单击编辑框控件,跳出输入法。如图1所示:

Qt输入法设计(嵌入式)_第1张图片

图1 输入法效果

Qt输入法设计(嵌入式)_第2张图片

图2 开发板运行效果

【原理】将QToolButton控件上的字符,传给需要输入的控件。

【简介】

先利用QT Creator设计一个键盘界面,如图2所示:

图2 设计键盘界面

以字符为'q’的按钮为例,将对象命名为toolButton_q,以此类推。

【关键代码】

keyboard.h

#ifndef  KeyBoard_H
#define  KeyBoard_H

#include <QtGui>
#include <QSignalMapper>
#include "ui_keyboard.h"

enum {
    iMode_Normal = 0,
    iMode_Passwd = 1
};

namespace Ui {
    class KeyBoard;
}

class KeyBoard : public QDialog, Ui::KeyBoard
{
    Q_OBJECT

public:
     KeyBoard();
     ~KeyBoard();

     //void mouseMoveEvent(QMouseEvent *);
     //void mousePressEvent(QMouseEvent *);

public:
     QString text;

private:
     QSignalMapper *signalMapper;
     QPoint dragPosition;
     bool caps_Lock;
     int inputMode;
     bool waitingForOperand;

private:
     void setMapper();
     void connectMapper();

signals:
     void setvalue(const QString &);

private slots:
     void onCursorRight();
     void onCursorLeft();
     void onCursorUp();
     void onCursorDown();

     void onHorizontalSliderValueChanged(int value);
     void setDispText(const QString& text);
     void onInvMode();

     void onCapslock();
     void onEnter();
     void onBackspace();
     void onEsc();
};

#endif //  KeyBoard_H


keyboard.cpp

#include <QtGui>
#include "keyboard.h"

KeyBoard::KeyBoard()
{
    setupUi(this);
    setWindowFlags(Qt::Tool|Qt::WindowStaysOnTopHint|Qt::FramelessWindowHint);
    display->setFocus();
    waitingForOperand = true;
    inputMode = iMode_Normal;
    caps_Lock = false;

    signalMapper=new QSignalMapper(this);
    setMapper();
    connectMapper();

    connect(signalMapper,SIGNAL(mapped(const QString&)),this,SLOT(setDispText(const QString&)));

    connect(toolButton_up,SIGNAL(clicked()),this,SLOT(onCursorUp()));
    connect(toolButton_down,SIGNAL(clicked()),this,SLOT(onCursorDown()));
    connect(toolButton_right,SIGNAL(clicked()),this,SLOT(onCursorRight()));
    connect(toolButton_left,SIGNAL(clicked()),this,SLOT(onCursorLeft()));

    connect(toolButton_Inv,SIGNAL(clicked()),this,SLOT(onInvMode()));
    connect(toolButton_enter,SIGNAL(clicked()),this,SLOT(onEnter()));
    connect(toolButton_backspace,SIGNAL(clicked()),this,SLOT(onBackspace()));
    connect(toolButton_capslock,SIGNAL(clicked()),this,SLOT(onCapslock()));
    connect(toolButton_esc,SIGNAL(clicked()),this,SLOT(onEsc()));
    connect(horizontalSlider,SIGNAL(valueChanged(int)),this,SLOT(onHorizontalSliderValueChanged(int)));
}

KeyBoard::~KeyBoard()
{

}


/*
* Name : void mouseMoveEvent(QMouseEvent *event),void mousePressEvent(QMouseEvent *event)
* Type : QEvent
* Func : realize drag the window of keyBoard by mouse press and move
* In   : QMouseEvent
* Out  : Null
*/

/*
void KeyBoard::mouseMoveEvent(QMouseEvent *event)
{
    if (event->buttons() == Qt::LeftButton)
    {
        move(event->globalPos() - dragPosition);
        event->accept();
    }
}

void KeyBoard::mousePressEvent(QMouseEvent *event)
{
    if (event->button() == Qt::LeftButton)
    {
        dragPosition = event->globalPos() - frameGeometry().topLeft();
        event->accept();
    }
    if (event->button() == Qt::RightButton)
    {
        close();
    }

}
*/

/*
* Name : void setMapper()
* Type : function
* Func : mapping the toolButton text to signalMapper
* In   : Null
* Out  : Null
*/
void KeyBoard::setMapper()
{
    //number
    signalMapper->setMapping(toolButton_0,toolButton_0->text());
    signalMapper->setMapping(toolButton_1,toolButton_1->text());
    signalMapper->setMapping(toolButton_2,toolButton_2->text());
    signalMapper->setMapping(toolButton_3,toolButton_3->text());
    signalMapper->setMapping(toolButton_4,toolButton_4->text());
    signalMapper->setMapping(toolButton_5,toolButton_5->text());
    signalMapper->setMapping(toolButton_6,toolButton_6->text());
    signalMapper->setMapping(toolButton_7,toolButton_7->text());
    signalMapper->setMapping(toolButton_8,toolButton_8->text());
    signalMapper->setMapping(toolButton_9,toolButton_9->text());

    //letter
    signalMapper->setMapping(toolButton_a,toolButton_a->text());
    signalMapper->setMapping(toolButton_b,toolButton_b->text());
    signalMapper->setMapping(toolButton_c,toolButton_c->text());
    signalMapper->setMapping(toolButton_d,toolButton_d->text());
    signalMapper->setMapping(toolButton_e,toolButton_e->text());
    signalMapper->setMapping(toolButton_f,toolButton_f->text());
    signalMapper->setMapping(toolButton_g,toolButton_g->text());
    signalMapper->setMapping(toolButton_h,toolButton_h->text());
    signalMapper->setMapping(toolButton_i,toolButton_i->text());
    signalMapper->setMapping(toolButton_j,toolButton_j->text());
    signalMapper->setMapping(toolButton_k,toolButton_k->text());
    signalMapper->setMapping(toolButton_l,toolButton_l->text());
    signalMapper->setMapping(toolButton_m,toolButton_m->text());
    signalMapper->setMapping(toolButton_n,toolButton_n->text());
    signalMapper->setMapping(toolButton_o,toolButton_o->text());
    signalMapper->setMapping(toolButton_p,toolButton_p->text());
    signalMapper->setMapping(toolButton_q,toolButton_q->text());
    signalMapper->setMapping(toolButton_r,toolButton_r->text());
    signalMapper->setMapping(toolButton_s,toolButton_s->text());
    signalMapper->setMapping(toolButton_t,toolButton_t->text());
    signalMapper->setMapping(toolButton_u,toolButton_u->text());
    signalMapper->setMapping(toolButton_v,toolButton_v->text());
    signalMapper->setMapping(toolButton_w,toolButton_w->text());
    signalMapper->setMapping(toolButton_x,toolButton_x->text());
    signalMapper->setMapping(toolButton_y,toolButton_y->text());
    signalMapper->setMapping(toolButton_z,toolButton_z->text());

    //other
    signalMapper->setMapping(toolButton_line1,toolButton_line1->text());
    signalMapper->setMapping(toolButton_line2,toolButton_line2->text());
    signalMapper->setMapping(toolButton_line3,toolButton_line3->text());
    signalMapper->setMapping(toolButton_wenhao,toolButton_wenhao->text());
    signalMapper->setMapping(toolButton_dot,toolButton_dot->text());
    signalMapper->setMapping(toolButton_space," ");
    signalMapper->setMapping(toolButton_tab,"\t");
}

/*
* Name : void connectMapper()
* Type : function
* Func : connect the toolButton signal(clicked()) to the signalMapper slots(map())
* In   : Null
* Out  : Null
*/
void KeyBoard::connectMapper()
{
    //number
    connect(toolButton_0,SIGNAL(clicked()),signalMapper,SLOT(map()));
    connect(toolButton_1,SIGNAL(clicked()),signalMapper,SLOT(map()));
    connect(toolButton_2,SIGNAL(clicked()),signalMapper,SLOT(map()));
    connect(toolButton_3,SIGNAL(clicked()),signalMapper,SLOT(map()));
    connect(toolButton_4,SIGNAL(clicked()),signalMapper,SLOT(map()));
    connect(toolButton_5,SIGNAL(clicked()),signalMapper,SLOT(map()));
    connect(toolButton_6,SIGNAL(clicked()),signalMapper,SLOT(map()));
    connect(toolButton_7,SIGNAL(clicked()),signalMapper,SLOT(map()));
    connect(toolButton_8,SIGNAL(clicked()),signalMapper,SLOT(map()));
    connect(toolButton_9,SIGNAL(clicked()),signalMapper,SLOT(map()));

    //letter
    connect(toolButton_a,SIGNAL(clicked()),signalMapper,SLOT(map()));
    connect(toolButton_b,SIGNAL(clicked()),signalMapper,SLOT(map()));
    connect(toolButton_c,SIGNAL(clicked()),signalMapper,SLOT(map()));
    connect(toolButton_d,SIGNAL(clicked()),signalMapper,SLOT(map()));
    connect(toolButton_e,SIGNAL(clicked()),signalMapper,SLOT(map()));
    connect(toolButton_f,SIGNAL(clicked()),signalMapper,SLOT(map()));
    connect(toolButton_g,SIGNAL(clicked()),signalMapper,SLOT(map()));
    connect(toolButton_h,SIGNAL(clicked()),signalMapper,SLOT(map()));
    connect(toolButton_i,SIGNAL(clicked()),signalMapper,SLOT(map()));
    connect(toolButton_j,SIGNAL(clicked()),signalMapper,SLOT(map()));
    connect(toolButton_k,SIGNAL(clicked()),signalMapper,SLOT(map()));
    connect(toolButton_l,SIGNAL(clicked()),signalMapper,SLOT(map()));
    connect(toolButton_m,SIGNAL(clicked()),signalMapper,SLOT(map()));
    connect(toolButton_n,SIGNAL(clicked()),signalMapper,SLOT(map()));
    connect(toolButton_o,SIGNAL(clicked()),signalMapper,SLOT(map()));
    connect(toolButton_p,SIGNAL(clicked()),signalMapper,SLOT(map()));
    connect(toolButton_q,SIGNAL(clicked()),signalMapper,SLOT(map()));
    connect(toolButton_r,SIGNAL(clicked()),signalMapper,SLOT(map()));
    connect(toolButton_s,SIGNAL(clicked()),signalMapper,SLOT(map()));
    connect(toolButton_t,SIGNAL(clicked()),signalMapper,SLOT(map()));
    connect(toolButton_u,SIGNAL(clicked()),signalMapper,SLOT(map()));
    connect(toolButton_v,SIGNAL(clicked()),signalMapper,SLOT(map()));
    connect(toolButton_w,SIGNAL(clicked()),signalMapper,SLOT(map()));
    connect(toolButton_x,SIGNAL(clicked()),signalMapper,SLOT(map()));
    connect(toolButton_y,SIGNAL(clicked()),signalMapper,SLOT(map()));
    connect(toolButton_z,SIGNAL(clicked()),signalMapper,SLOT(map()));

    //other
    connect(toolButton_line1,SIGNAL(clicked()),signalMapper,SLOT(map()));
    connect(toolButton_line2,SIGNAL(clicked()),signalMapper,SLOT(map()));
    connect(toolButton_line3,SIGNAL(clicked()),signalMapper,SLOT(map()));
    connect(toolButton_wenhao,SIGNAL(clicked()),signalMapper,SLOT(map()));
    connect(toolButton_dot,SIGNAL(clicked()),signalMapper,SLOT(map()));
    connect(toolButton_space,SIGNAL(clicked()),signalMapper,SLOT(map()));
    connect(toolButton_tab,SIGNAL(clicked()),signalMapper,SLOT(map()));
}

void KeyBoard::onCursorUp()
{
    QKeyEvent upPress(QEvent::KeyPress,Qt::Key_Up,Qt::NoModifier);
    QApplication::sendEvent(QApplication::focusWidget(),&upPress);
}

void KeyBoard::onCursorDown()
{
    QKeyEvent downPress(QEvent::KeyPress,Qt::Key_Down,Qt::NoModifier);
    QApplication::sendEvent(QApplication::focusWidget(),&downPress);
}

/*
* Name : void onCursorRight()
* Type : slot
* Func : cursor move right
* In   : Null
* Out  : Null
*/
void KeyBoard::onCursorRight()
{
    display->cursorForward(false,1);
}

/*
* Name : void onCursorLeft()
* Type : slot
* Func : cursor move left
* In   : Null
* Out  : Null
*/
void KeyBoard::onCursorLeft()
{
    display->cursorBackward(false,1);
}

/*
* Name : void onHorizontalSliderValueChanged(int value)
* Type : slot
* Func :
* In   : int
* Out  : Null
*/
void KeyBoard::onHorizontalSliderValueChanged(int value)
{
    horizontalSlider->setRange(0,255);
    qreal opacity=(qreal)((1.0/256.0)*value);
    setWindowOpacity(opacity);
    lcdNumber->display(value);
}

/*
* Name : void setDispText(const QString& text)
* Type : slot
* Func : insert and show text on display(QLineEdit)
* In   : QString
* Out  : Null
*/
void KeyBoard::setDispText(const QString& text)
{
    if(waitingForOperand)
    {
        display->clear();
        waitingForOperand = false;
    }
    display->insert(text);
}

/*
* Name : void onInvMode()
* Type : slot
* Func : invert input mode
* In   : Null
* Out  : Null
*/
void KeyBoard::onInvMode()
{
    if(inputMode == iMode_Normal)
    {
        inputMode = iMode_Passwd;
    }
    else if(inputMode == iMode_Passwd)
    {
        inputMode = iMode_Normal;
    }

    if(inputMode == iMode_Normal)
    {
        display->setEchoMode(QLineEdit::Normal);
    }
    else if(inputMode == iMode_Passwd)
    {
        display->setEchoMode(QLineEdit::Password);
    }
}

/*
* Name : void onCapslock()
* Type : slot
* Func : caps lock
* In   : Null
* Out  : Null
*/
void KeyBoard::onCapslock()
{
    caps_Lock = !caps_Lock;
    if(caps_Lock)
    {
        //letter
        toolButton_a->setText("A");
        toolButton_b->setText("B");
        toolButton_c->setText("C");
        toolButton_d->setText("D");
        toolButton_e->setText("E");
        toolButton_f->setText("F");
        toolButton_g->setText("G");
        toolButton_h->setText("H");
        toolButton_i->setText("I");
        toolButton_j->setText("J");
        toolButton_k->setText("K");
        toolButton_l->setText("L");
        toolButton_m->setText("M");
        toolButton_n->setText("N");
        toolButton_o->setText("O");
        toolButton_p->setText("P");
        toolButton_q->setText("Q");
        toolButton_r->setText("R");
        toolButton_s->setText("S");
        toolButton_t->setText("T");
        toolButton_u->setText("U");
        toolButton_v->setText("V");
        toolButton_w->setText("W");
        toolButton_x->setText("X");
        toolButton_y->setText("Y");
        toolButton_z->setText("Z");
        toolButton_line1->setText("<");
        toolButton_line2->setText(">");
        toolButton_line3->setText("-");
        toolButton_dot->setText("*");
        toolButton_wenhao->setText("#");
    }
    else
    {
        //letter
        toolButton_a->setText("a");
        toolButton_b->setText("b");
        toolButton_c->setText("c");
        toolButton_d->setText("d");
        toolButton_e->setText("e");
        toolButton_f->setText("f");
        toolButton_g->setText("g");
        toolButton_h->setText("h");
        toolButton_i->setText("i");
        toolButton_j->setText("j");
        toolButton_k->setText("k");
        toolButton_l->setText("l");
        toolButton_m->setText("m");
        toolButton_n->setText("n");
        toolButton_o->setText("o");
        toolButton_p->setText("p");
        toolButton_q->setText("q");
        toolButton_r->setText("r");
        toolButton_s->setText("s");
        toolButton_t->setText("t");
        toolButton_u->setText("u");
        toolButton_v->setText("v");
        toolButton_w->setText("w");
        toolButton_x->setText("x");
        toolButton_y->setText("y");
        toolButton_z->setText("z");
        toolButton_line1->setText("/");
        toolButton_line2->setText("\\");
        toolButton_line3->setText("_");
        toolButton_dot->setText(".");
        toolButton_wenhao->setText("?");
    }
    setMapper();
}

/*
* Name : void onEnter()
* Type : slot
* Func : enter and emit the signal to editor to set text
* In   : Null
* Out  : Null
*/
void KeyBoard::onEnter()
{
    waitingForOperand = true;
    text = display->text();
    emit setvalue(text);
    display->clear();
    this->close();
}

/*
* Name : void onEsc()
* Type : slot
* Func : Esc
* In   : Null
* Out  : Null
*/
void KeyBoard::onEsc()
{
    display->clear();
    text.clear();
    this->close();
}

/*
* Name : void onBackspace()
* Type : slot
* Func : backspace
* In   : Null
* Out  : Null
*/
void KeyBoard::onBackspace()
{
    display->backspace();
}


inputmethod.h

#ifndef INPUTMETHOD_H
#define INPUTMETHOD_H

#include "keyboard.h"

class InputMethod:public KeyBoard
{
    Q_OBJECT
public:
    InputMethod();
    ~InputMethod();

    bool eventFilter(QObject *obj, QEvent *event);

public:
    KeyBoard *keyboard;

public:
    void showKeyBoard();

signals:
    void setImObjName(QString);


};

#endif // INPUTMETHOD_H


inputmethod.cpp

#include <QDebug>
#include "inputmethod.h"

InputMethod::InputMethod()
{
    keyboard = new KeyBoard;
    setWindowFlags(Qt::Tool|Qt::WindowStaysOnTopHint|Qt::FramelessWindowHint);
}

InputMethod::~InputMethod()
{
    delete keyboard;
}

/*
* Name : void eventFilter(QObject *obj, QEvent *event);
* Type : QEvent
* Func : judge input method event
* In   : QObject,QEvent
* Out  : bool
*/
bool InputMethod::eventFilter(QObject *obj, QEvent *event)
{

    if(event->type()==QEvent::MouseButtonPress)
    {
        emit setImObjName(obj->objectName()); //send the Object Name of event to judge who trigger it
        //qDebug() <<obj->objectName();
        showKeyBoard();
        return true;
    }

    return QObject::eventFilter(obj,event);
}


/*
* Name : void showKeyBoard();
* Type : function
* Func : show keyBoard
* In   : Null
* Out  : Null
*/
void InputMethod::showKeyBoard()
{
    keyboard->setWindowFlags(Qt::Tool|Qt::WindowStaysOnTopHint|Qt::FramelessWindowHint);
    keyboard->move(50,120);
    keyboard->exec();
}



 

【使用方法】

以QLineEdit为例说明:

(1) 首先,在需要输入法的控件文件中包含inputmethod.h文件;

#include "inputmethod.h"

(2) 创建输入法对象

im = new InputMethod;

(3) QLineEdit对象添加事件机制

managerEdit->installEventFilter(im);
passwdEdit->installEventFilter(im);

managerEdit->setObjectName("managerEdit");
passwdEdit->setObjectName("passwdEdit");

connect(im,SIGNAL(setImObjName(QString)),this,SLOT(createImConnect(QString)));//connect the input method


该方法是通过判断对象名,实现在不同控件中切换。所以一定要给对象命名。

(4) 槽函数createImConnect(QString ObjName)的编写

void QLogin::createImConnect(QString ObjName)
{
    disconnect(im->keyboard,SIGNAL(setvalue(QString)),passwdEdit,SLOT(setText(QString)));
    disconnect(im->keyboard,SIGNAL(setvalue(QString)),managerEdit,SLOT(setText(QString)));

    if(ObjName=="managerEdit")
    {
        connect(im->keyboard,SIGNAL(setvalue(QString)),managerEdit,SLOT(setText(QString)));
    }
    else
    {

        connect(im->keyboard,SIGNAL(setvalue(QString)),passwdEdit,SLOT(setText(QString)));
    }

}

 

【示例下载地址】

 http://download.csdn.net/detail/tandesir/4337488(有多个版本,个人推荐的版本http://download.csdn.net/detail/tandesir/4378244)

(2012-6-3)

*该资源是针对嵌入式开发板的版本,只能输入英文和数字,以及少数的符号。虽然可以跨平台运行,但基于X86系统是不合适的,至少不会获得良好的用户体验,好像只是界面交互。上传资源只是为了学习交流,如果是为了做基于Windows XP的输入法,参考价值不大。

*CSDN的<资源>功能,无法修改,也无法删除。上传资源者请慎重,至少去掉个人信息。希望CSDN可以赋予用户对资源修改、删除的权限。

 

【写在最后】 

看到他人对资源的评论,觉得很有趣。有人说是界面交互。有人说是短信机。严格来讲,这确实不能和PC机上输入法相比,原因如下:

(1) 只能输入少数英文字符;

(2) 关联特定的控件(见设计模式(1)-模板模式(Template) ,个人推荐这个版本);

(3) 没有事先预置的词库。

但我认为这种做法,在嵌入式平台还是有价值的。其一,它占用很少的资源;其二,确实能有效的实现英文的输入。可以说是界面交互。但短信机的称呼就不正确,因为没有实现发短信的功能,如果有必要可以加上,呵呵。

 

 

转载请标明出处,仅供学习交流,勿用于商业目的

Copyright @ http://blog.csdn.net/tandesir

 

 




 

 

 

 

你可能感兴趣的:(function,null,输入法,qt,Signal,keyboard)