Qt5 soft keyboard implements Chinese pinyin input method

directory (?)[+]

1. Introduction

The virtual keyboard implementation based on Qt seems to be almost all based on the generated Chinese character database, which is not flexible and intelligent enough.

2. Realization

1. The library of pinyin input method

We use the Google Pinyin input method at the bottom of Android. After Qt5.7, Qt officially released an input method (supporting Chinese Pinyin input), but it was designed based on QML. We got its source code and found that the Pinyin in it The implementation of the input method also uses the Google Pinyin underlying Android.

It must be remembered: in the case of wheels, we must never build our own wheels unless we have to. We stripped out the source code, implemented in C++, and Qt officially modified a few lines of code, which can be used directly on Qt. We separately compile the Google Pinyin input method into a static library, and link it directly when the soft keyboard is implemented.

googlepinyin.pro 
QT       -= gui
TARGET = googlepinyin
TEMPLATE = lib
CONFIG += staticlib
SOURCES += \
    dictbuilder.cpp \
    dictlist.cpp \
    dicttrie.cpp \
    lpicache.cpp \
    matrixsearch.cpp \
    mystdlib.cpp \
    ngram.cpp \
    pinyinime.cpp \
    searchutility.cpp \
    spellingtable.cpp \
    spellingtrie.cpp \
    splparser.cpp \
    sync.cpp \
    userdict.cpp \
    utf16char.cpp \
    utf16reader.cpp
HEADERS += \
    atomdictbase.h \
    dictbuilder.h \
    dictdef.h \
    dictlist.h \
    dicttrie.h \
    lpicache.h \
    matrixsearch.h \
    mystdlib.h \
    ngram.h \
    pinyinime.h \
    searchutility.h \
    spellingtable.h \
    spellingtrie.h \
    splparser.h \
    sync.h \
    userdict.h \
    utf16char.h \
    utf16reader.h

The project source code is here googlepinyin.7z , you can download and study by yourself. The interface provided to the outside world is in the pinyinime.h file.

2. Implement the software disk

First look at a screenshot of the implemented software disk. 
Software screenshot

There is a lot of information on the Internet of the soft keyboard plug-in implementation framework, so I won't go into details. 
My source code project directory is as follows: 
Source code project directory

dict:存放最后要使用的二进制字典文件,此字典文件是谷歌拼音输入法根据最原始的汉字文件生成的,大家到时候直接使用就行了,不用深究此处。 
font:存放图标字体,FontAwesome.otf。Font Awesome 是一款可缩放的矢量图标字库。相信很多人对这个感到陌生,请大家自行搜索FontAwesome,以及如何在Qt中运用FontAwesome。 
images:按钮图片背景。 
pinyin:刚才编译出来的谷歌拼音库和头文件。 
googlepinyin.h,googlepinyin.cpp:谷歌拼音库的接口使用文件。 
keyboardform.h,keyboardform.cpp:键盘布局文件。 
keyeventdispacher.h,keyeventdispacher.cpp:键盘按键的事件处理文件。 
inputcontext.,platforminputcontextplugin.,Qt5Input.json,virtualkeyborad_global.h:这几个文件不做赘述。 
res.qrc:资源文件

工程文件中难点不多,不再对源码进行解释。

编译这个工程VritualKeyboard.7z,最后生成的是一个动态链接库,但是如何使用呢?

3、使用软键盘

  • 假设要使用输入法的程序是QtDemo.exe;
  • Create a new folder platforminputcontexts in the directory where Qt5Demo.exe is located (the folder name cannot be changed to something else), and put VirtualKeyboard.dll into it;
  • Copy the entire dict folder in the project directory to the directory where Qt5Demo.exe is located;
  • Finally, run QtDemo.exe. 
    test directory

3. Summary

1. Don't reinvent the wheel. 
2. Use Font Awesome icon fonts in Qt projects. 
3. Learn to use the template project of the input method plug-in. 
4. Learn to use Google Pinyin input method.

4. Thanks

  1. Thanks to all the great gods on QTCN
  2. Thanks to GitHub , the world's largest gay dating site

perfect ending

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324682818&siteId=291194637