Qt 基于Google引擎的拼音输入法

效果演示 :

开发板输入需要用到软键盘,Qt自带的虚拟键盘时qml写的,可惜我不会qml,移植到开发板上有各种问题还找不到原因,索性自己写一个简单的软键盘,用的是Google输入法源码,练手所做水平有限。有疑问欢迎留言商讨,共同进步。

源码地址:https://github.com/zuotian0222/InputMethod.git

实现思路:

1.实现自定义按钮KeyButton,继承自QPushButton,简单的添加了两个私有变量,int key;QString value,用来存放键值和字符,添加信号,当按钮按下发送key和value;

{
    /*支持长按*/
    this->setAutoRepeat(true);
    this->setAutoRepeatDelay(500);
    this->setAutoRepeatInterval(100);

    connect(this,&QPushButton::pressed,this,&KeyButton::slot_onPressed);
}

void KeyButton::setValue(QString value)
{
    this->value = value;
    this->setText(value);
}

void KeyButton::setKey(int key)
{
    this->key = key;
}

void KeyButton::slot_onPressed()
{
    emit key_pressed(key,value);
}

2.实现键盘KeyBoard,在UI文件中添加适量按钮并合理布局,需要按键响应的按钮提升为KeyButton;添加中文输入框和中文候选框,候选框用的是QListWidget 设置为横向排列,水平滚动;将按键响应的按钮添加进列表管理,列表添加 小写,大写,数字字符三种状态的键值和输出字符,切换状态时通过列表遍历按钮进行加载,绑定按钮点击信号,接收键值和输出字符,

 //按钮绑定槽函数
    for(KeyButton * btn : keyBtnList){
        connect(btn,&KeyButton::key_pressed,this,&KeyBoard::slot_key_keyEvent);
    }



void KeyBoard::slot_key_keyEvent(int key,QString value)
{

    QKeyEvent keyPress(QEvent::KeyPress, key, Qt::NoModifier, value);

    QApplication::sendEvent(edit, &keyPress);
}

通过键盘事件发送出去,edit为接收的键盘事件的控件。

3.中文输入,Input类包含Google输入法源码,init函数加载两个dat文件,无则从资源文件中拷贝,设置拼音、输出最大长度;search函数通过传入拼音字符串返回匹配到的汉字个数 get_candidate函数返回一定的汉字列表,再将汉字列表显示到候选框即可。对Google输入法源码了解颇浅,没有实现拼音拆分匹配,仅在选中汉字后全部清空。

bool Input::init(int max_spell_len, int max_out_len)
{
    QString path = QDir::currentPath();
    qDebug()<<path;

    QFile file_pinyin(dict_path);
    if(!file_pinyin.exists()){
        QFile::copy(":/dict/dict_pinyin.dat", path+"/"+dict_path);
        QFile::setPermissions(path,QFileDevice::ReadOther|QFileDevice::WriteOther);
    }
    QFile file_user(dict_user_path);
    if(!file_user.exists()){
        QFile::copy(":/dict/dict_pinyin_user.dat",path+"/"+dict_user_path);
        QFile::setPermissions(path,QFileDevice::ReadOther|QFileDevice::WriteOther);
    }

    m_spell_len = max_spell_len;
    m_out_len = max_out_len;

    bool ret = im_open_decoder(QString("%1/"+dict_path).arg(path).toLocal8Bit().data(),
                               QString("%1/"+dict_user_path).arg(path).toLocal8Bit().data());
    if(!ret)
        return ret;

    im_set_max_lens(static_cast<size_t>(m_spell_len),static_cast<size_t>(m_out_len));
    reset_search();

    m_able = ret;
    return ret;
}


unsigned int Input::search(const QString spell)
{
    if (!m_able)
        return 0;
    QByteArray bytearray;
    char    *pinyin;

    bytearray = spell.toUtf8();
    pinyin = bytearray.data();

    size_t candnum = im_search(pinyin, static_cast<size_t>(bytearray.size()));

    if(static_cast<int>(candnum) < m_out_len){
        return static_cast<unsigned int>(candnum);
    }
    return static_cast<unsigned int>(m_out_len);
}


QStringList Input::get_candidate(unsigned int candnum)
{   
    QStringList textList;
    if(candnum == 0)
        return textList;

    char16 * cand_buf = new char16[m_out_len];
    for (unsigned int i = 0; i < candnum; i++) {
        char16 *cand;
        cand = im_get_candidate(i, cand_buf, static_cast<unsigned int>(m_out_len));
        if(cand){
            textList.append(QString::fromUtf16(cand));
        }
        else {
            continue;
        }
    }
    delete[] cand_buf;
    //qDebug()<<textList;
    return textList;
}

4.使用,需要输入的控件通过setEdit函数传入。

    keyboard = new KeyBoard();
    keyboard->resize(600,300);
    ui->lineEdit->installEventFilter(this);
    ui->lineEdit_2->installEventFilter(this);
    ui->lineEdit_3->installEventFilter(this);


bool Widget::eventFilter(QObject *w, QEvent *e)
{
    if(w->inherits("QLineEdit") && e->type() == QEvent::MouseButtonRelease){
        QWidget * p = qobject_cast<QWidget *>(w);
        keyboard->setEdit(p);
        QPoint posA = p->mapToGlobal(QPoint(0, 0));
        keyboard->move(posA.x(),posA.y()+p->height());
        keyboard->show();
    }
    return QWidget::eventFilter(w,e);
}

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值