QT 使用自定义输入法

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",
                                   "@", "#", "_", "\"", "“", "”", ",", ",", ".", "。",
                                   ";", ";", ":", ":", "'", "’", "、", "!", "!",
                                   "~", "~", "+", "-", "*", "/", "=", "÷", "×", "√",
                                   "`", "?", "^", "&&", "%", "|", "(", ")", "(", ")",
                                   "[", "]", "【", "】", "{", "}", "<", ">"
  • 2
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 12
    评论
Qt中嵌入自定义输入是通过使用QInputMethod类来实现的。首先,我们需要实现一个自定义输入,并将其集成到Qt应用程序中。 首先,我们需要创建一个继承自QInputMethod的自定义输入类。在该类中,我们可以重写一些虚拟函数来实现输入的具体功能,例如inputMethodQuery和commit。 在inputMethodQuery函数中,我们可以处理诸如查询当前输入状态、候选词列表等的请求。我们可以根据应用程序的需求来处理这些查询,并返回相应的结果。 在commit函数中,我们可以获取用户输入的文本并将其提交到应用程序中。我们可以根据输入的设计来处理文本的提交,例如在特定条件下才提交或者进行一些预处理操作。 然后,在我们的应用程序中,我们需要创建一个QInputMethod对象,并将其设置为QWidget的输入。我们可以使用QWidget的setInputMethod函数来实现这一点。 接下来,我们需要在QWidget的事件处理函数中处理输入的相关事件。例如,当输入的状态变化时,我们可以通过重写QWidget的inputMethodEvent函数来获取并处理输入事件。 最后,我们可以通过重新编译和运行应用程序来测试我们的自定义输入是否成功嵌入到了Qt应用程序中。我们可以观察输入的效果,并根据需要进行调整和优化。 总而言之,通过使用QInputMethod类和相关的函数,我们可以在Qt中嵌入自定义输入。我们可以通过实现自定义输入类,并将其与应用程序的QWidget相关联,来实现自定义输入的功能和特性。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 12
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值