QT 使用自定义输入法

在Linux root环境下,由于无法使用fcitx输入法框架,本文介绍了如何在QT中使用自定义输入法,包括编译googlepinyin静态库,编译插件,加载插件,连接信号与槽以及修改插件的详细步骤。此外,还提到了一个由tgtsml编写的Qt5输入法实现的GitHub资源。
摘要由CSDN通过智能技术生成

QT 使用自定义输入法


因在Linux中,root环境下无法使用fcitx输入法框架,也就无法使用输入法,这个时候只好使用自己定义的输入法,这样,即使是在root下,也能输入中文。

1 使用插件


1)编译googlepinyin静态库

QT          -= gui
TEMPLATE    = lib

CONFIG      += staticlib
TARGET      = googlepinyin
win32{
   
    CONFIG      += debug_and_release build_all
    CONFIG(debug, debug|release){
   
        TARGET  = ../../plugin/googlepinyin/$$join(TARGET,,,d)
    }
    CONFIG(release, debug|release){
   
        TARGET  = ../../plugin/googlepinyin/$$TARGET
    }
}
unix{
   
    TARGET      = ../plugin/googlepinyin/$$TARGET
    MOC_DIR     = ../tmpfiles
    RCC_DIR     = ../tmpfiles
    UI_DIR      = ../tmpfiles
    OBJECTS_DIR = ../tmpfiles
}

编译得到libgooglepinyin.a静态库。

2)编译plugin

将上一步编译获得的libgooglepinyin.a放到plugin项目的googlepinyin目录下,开始编译。

QT          = core gui-private widgets

TEMPLATE    = lib
TARGET      = tgtsmlInputContextPlugin

INCLUDEPATH += $$PWD/googlepinyin

win32{
   
    CONFIG      += debug_and_release build_all

    CONFIG(debug, debug|release){
   
        TARGET  = ../../debug/platformInputContexts/$$join(TARGET,,,d)
        LIBS    += -L$$PWD/googlepinyin/ -lgooglepinyind
    }CONFIG(release, debug|release){
   
        TARGET  = ../../release/platformInputContexts/$$TARGET
        LIBS    += -L$$PWD/googlepinyin/ -lgooglepinyin
    }
}
unix{
   
    TARGET      = ../target/$$TARGET
    LIBS        += -L$$PWD/googlepinyin/ -lgooglepinyin
    MOC_DIR     = ../tmpfiles
    RCC_DIR     = ../tmpfiles
    UI_DIR      = ../tmpfiles
    OBJECTS_DIR = ../tmpfiles
}

获得插件libtgtsmlInputContextPlugin.so

3)加载插件

./dict目录下放置两个dat词典文件;
.pro中添加相应库;

LIBS += -L./plugins -ltgtsmlInputContextPlugin

widget.h添加成员变量及LoadPlugins()方法;

bool LoadPlugins();

TgtsmlPlatformInputContext *m_input_chinese;

widget.cpp

bool NewPatientPage::LoadPlugins()
{
   
    QString pluginpath("./plugins/libtgtsmlInputContextPlugin.so");
    QFile file("./plugins/libtgtsmlInputContextPlugin.so");
    if (!file.exists())
    {
   
        QMessageBox::warning(this,tr("错误信息"),tr("找不到%1文件").arg(pluginpath));
        return false;
    }
    QPluginLoader loader(pluginpath);
    QObject *instance = loader.instance(); 
    if (instance!= nullptr)
    {
   
        qDebug()<<pluginpath+" is loaded";
        TgtsmlPlatformInputContextPlugin *avc = qobject_cast<TgtsmlPlatformInputContextPlugin *>(instance);
        m_input_chinese =  avc->create("tgtsml",QStringList());
        return true;
    }
    else {
   
        QMessageBox::information(this,"failed to load Chinese input plugin",loader.errorString());
    }
    // 需要手动释放
    delete instance;
    return true;
}

4)连接相应的信号与槽

重载QLineEditLineEdit.h

class LineEdit : public QLineEdit
{
   
    Q_OBJECT
public:
    LineEdit(QWidget* parent=nullptr);
    virtual ~LineEdit()=default;

signals:
    void SendObject(QObject *obj);
    void showInputPanel();
    void hideInputPanel();
    void KeyPress(QKeyEvent *e);

protected:
    virtual void focusInEvent(QFocusEvent*e);
    virtual void focusOutEvent(QFocusEvent*e);
    virtual void keyPressEvent(QKeyEvent *e);
    virtual void mousePressEvent(QMouseEvent*e);
};

LineEdit.cpp

#include "LineEdit.h"
//---------------------------------
LineEdit::LineEdit(QWidget *parent)
    :QLineEdit(parent)
{
   
}
//---------------------------------
void LineEdit::focusInEvent(QFocusEvent *e)
{
   
    emit SendObject(this);
//    emit showInputPanel();
    return QLineEdit::focusInEvent(e);
}

void LineEdit::focusOutEvent(QFocusEvent *e)
{
   
    emit hideInputPanel();
    return QLineEdit::focusOutEvent(e);
}

//---------------------------------
void LineEdit::keyPressEvent(QKeyEvent *e)
{
   
    if(e->key()==Qt::Key_Backspace){
   
        if(e->modifiers().testFlag(Qt::NoModifier)){
   
            emit KeyPress(e);
            return;
        }
        else if(e->modifiers().testFlag(Qt::ShiftModifier)){
   
            return;
        }else if(e->modifiers().testFlag(Qt::ControlModifier)){
   
            QKeyEvent *p= new QKeyEvent(QEvent::KeyPress, Qt::Key_Backspace, Qt::NoModifier);
            QLineEdit::keyPressEvent(p);
            return;
        }
    }

    emit KeyPress(e);

    QLineEdit::keyPressEvent(e);

    if((e->key()>=Qt::Key_A && e->key()<=Qt::Key_Z) || (e->key()>=Qt::Key_1 && e->key()<=Qt::Key_5) ){
   
        QKeyEvent *p= new QKeyEvent(QEvent::KeyPress, Qt::Key_Backspace, Qt::NoModifier);
        QLineEdit::keyPressEvent(p);
    }
    switch (e->key()){
   
    case Qt::Key_Comma: case Qt::Key_Period: case Qt::Key_Slash: case Qt::Key_Space:
    case Qt::Key_Minus : case Qt::Key_Equal:
        QKeyEvent *p= new QKeyEvent(QEvent::KeyPress, Qt::Key_Backspace, Qt::NoModifier);
        QLineEdit::keyPressEvent(p);
        break;
    }
}
//---------------------------------
void LineEdit::mousePressEvent(QMouseEvent *e)
{
   
    emit showInputPanel();
    return QLineEdit::mousePressEvent(e);
}

widget.cpp

    connect(m_edit["doctor"], SIGNAL(SendObject(QObject*)), m_input_chinese, SLOT(setFocusObject(QObject *)));
    connect(m_edit["doctor"], SIGNAL(showInputPanel()), m_input_chinese, SLOT(showInputPanel()));
    connect(m_edit["doctor"], SIGNAL(hideInputPanel()), m_input_chinese, SLOT(hideInputPanel()));
    connect(m_edit["doctor"], SIGNAL(KeyPress(QKeyEvent *)), m_input_chinese, SLOT(KeyPress(QKeyEvent *)));

5)修改插件

修改KeyboardForm.h

#include "keyboardform.h"
#include <QLabel>
#include <QPushButton>
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QFontDatabase>
#include <QFile>
#include <QDebug>
#include <QApplication>
#include <QMouseEvent>
#include "pinyinime.h"

#include <iostream>

using std::initializer_list;
//using std::pair;
using namespace ime_pinyin;

#define chinesecharacters_number    7
const char *keyboard_characters = "qwertyuiopasdfghjklzxcvbnm,.?";
const QString keyboard_symbols[] = {
   "1", "2", "3", "4", "5", "6", "7", "8", "9", "0",
                                   "@", "#", "_", "\"", "“", "”", ",", ",", ".", "。",
                                   ";", ";", ":", ":", "'", "’", "、", "!", "!",
                                   "~", "~", "+", "-", "*", "/", "=", "÷", "×", "√",
                                   "`", "?", "^", "&&", "%", "|", "(", ")", "(", ")",
                                   "[", "]", "【", "】", "{", "}", "<", ">"
评论 12
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值