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",
"@", "#", "_", "\"", "“", "”", ",", ",", ".", "。",
";", ";", ":", ":", "'", "’", "、", "!", "!",
"~", "~", "+", "-", "*", "/", "=", "÷", "×", "√",
"`", "?", "^", "&&", "%", "|", "(", ")", "(", ")",
"[", "]", "【", "】", "{", "}", "<", ">"