0、背景
在使用QT進(jìn)行嵌入式應(yīng)用開發(fā)時(shí),往往程序最終的運(yùn)行設(shè)備是有觸屏的設(shè)備。因此,不可避免的會(huì)遇到虛擬鍵盤和中文輸入的問題。QT自帶一個(gè)虛擬鍵盤,可以在構(gòu)建套件/plugins/platforminputcontexts目錄下看到2個(gè)文件:
- qtvirtualkeyboardplugin.dll
- qtvirtualkeyboardplugin.dll.debug
QT自帶的虛擬鍵盤屏幕占比較大,而且很容易擋住輸入框,默認(rèn)不能拖動(dòng),不太完美。本就介紹一種替代方式,就是在QT程序中使用GooglePinyin輸入法實(shí)現(xiàn)虛擬鍵盤和中文錄入。
1、環(huán)境
操作系統(tǒng):windows10專業(yè)版
QT版本:5.15.2
QT安裝目錄:D:\Qt
QT編譯套件1:MinGW64
QT編譯套件2:MSVC2019_64
2、下載QtInputMethod_GooglePinyin源碼
1、下載地址:
https://gitee.com/tgtsml/QtInputMethod_GooglePinyin
2、下載后可以看到壓縮包如下圖:
3、解壓縮后如下圖:
4、文件夾的內(nèi)容如下:
3、使用MinGW64構(gòu)建套件編譯
3.1 編譯QtInputMethod_GooglePinyin源碼
1、通過qtcreator打開tgtsmlInputContextPlugin.pro工程,本例采用的MinGW64構(gòu)建套件。如下圖:
2、然后分別以debug模式和release模式對tgtsmlInputContextPlugin進(jìn)行重新構(gòu)建,會(huì)在tgtsmlInputContextPlugin.pro同級目錄下生成一個(gè)build目錄,如下圖:
3.2、部署tgtsmlInputContextPlugin輸入法插件
1、把上一步編譯生成的build/bin/platforminputcontexts目錄下的
- tgtsmlInputContextPlugin.dll
- tgtsmlInputContextPlugind.dll
如下圖:
復(fù)制到D:\Qt\5.15.2\mingw81_64\plugins\platforminputcontexts目錄下。
3.3、運(yùn)行測試程序
1、可以看到下載的源碼中包括一個(gè)測試程序demo,關(guān)鍵引入輸入法虛擬鍵盤的代碼如下:
qputenv("QT_IM_MODULE", QByteArray("tgtsml"));
如下圖:
這樣當(dāng)光標(biāo)聚焦在輸入文本框時(shí),會(huì)自動(dòng)彈出虛擬鍵盤。
運(yùn)行效果如下:
4、使用MSVC2019_64構(gòu)建套件編譯
4.1 編譯QtInputMethod_GooglePinyin源碼
1、在項(xiàng)目選項(xiàng)卡中對當(dāng)前項(xiàng)目的構(gòu)建套件切換至MSVC2019_64構(gòu)建套件上,然后對項(xiàng)目進(jìn)行重新構(gòu)建,然后這時(shí)出現(xiàn)了很多錯(cuò)誤,如下圖:
2、解決這些問題需要做2個(gè)方面的修改。
2.2.1 第1是把所有pro的換行符由Unix(LF)改為Windows(CRLF)
如下,原來是這樣:
改成下面這樣:
2.2.2 第2是在demo.pro、googlepinyin.pro、plugin.pro 3個(gè)文件中增加中文處理的配置,如下:
# 解決msvc構(gòu)建套件下qtcreator控制臺(tái)日志中文亂碼問題
msvc {
QMAKE_CFLAGS += /utf-8
QMAKE_CXXFLAGS += /utf-8
}
如下圖所示:
3、然后再重新構(gòu)建,就能正常編譯過去了。同樣使用debug模式和release模式編譯完畢后會(huì)在會(huì)在tgtsmlInputContextPlugin.pro同級目錄下生成一個(gè)build目錄,如果之前在MinGW64構(gòu)建套件下已生成了,建議先刪除,或者在pro文件中指定使用不同構(gòu)建套件時(shí)生成的庫放到不同位置,如下:
# 指定輸出目錄
!msvc {
DESTDIR = ../../wongoing_plugin_bin
}
msvc {
DESTDIR = ../../wongoing_plugin_bin_msvc
}
4.2、部署tgtsmlInputContextPlugin輸入法插件
1、把上一步編譯生成的build/bin/platforminputcontexts目錄下的
- tgtsmlInputContextPlugin.dll
- tgtsmlInputContextPlugind.dll
- tgtsmlInputContextPlugind.pdb
如下圖:
復(fù)制到D:\Qt\5.15.2\msvc2019_64\plugins\platforminputcontexts目錄下。
4.3、運(yùn)行測試程序,與步驟3.3相同。
5、修改QtInputMethod_GooglePinyin源碼解決在分屏情況下虛擬鍵盤顯示越界及不能跨屏顯示的問題
1、當(dāng)我的筆記本電腦有外接了一個(gè)顯示時(shí)(擴(kuò)展模式)使用時(shí)會(huì)出現(xiàn)2個(gè)問題,第1個(gè)問題是虛擬鍵盤不能顯示到擴(kuò)展屏幕上,雖然我的demo窗口顯示在擴(kuò)展屏幕。第2個(gè)問題,就是當(dāng)demo窗口(輸入框窗口)在主屏幕最右測時(shí),虛擬鍵盤會(huì)有一部分顯示不出來,就是顯示越界。具體如下圖:
上圖是虛擬鍵盤不能跨屏幕顯示,只能顯示在主屏上。
上圖是右側(cè)顯示不完整的情況。
2、修改plugin項(xiàng)目中的tgtsmlplatforminputcontext.cpp文件中的void TgtsmlPlatformInputContext::showInputPanel()中的代碼,如下:
void TgtsmlPlatformInputContext::showInputPanel()
{
if(!m_keyboard){
m_keyboard = new KeyboardForm;
connect(m_keyboard, &KeyboardForm::sendKeyToFocusItem, this, &TgtsmlPlatformInputContext::sendKeyToFocusItem);
}
if(m_keyboard->isHidden())m_keyboard->show();
QWidget *widgetTmp = qobject_cast<QWidget*>(m_focusitem);
if(widgetTmp){
//注釋以下代碼,因?yàn)闆]有考慮擴(kuò)展屏幕的坐標(biāo)問題
// QPoint widgetGlobalPos = widgetTmp->mapToGlobal(QPoint(0, 0));
// if(widgetGlobalPos.x() < 0){
// widgetGlobalPos.setX(0);
// }
// if(widgetGlobalPos.y() < 0){
// widgetGlobalPos.setY(0);
// }
// if(qApp->desktop()->width() - widgetGlobalPos.x() < m_keyboard->width()){
// widgetGlobalPos.setX(qApp->desktop()->width() - m_keyboard->width());
// }
// if(qApp->desktop()->height() - widgetGlobalPos.y() - 30 < m_keyboard->height()){
// widgetGlobalPos.setY(widgetGlobalPos.y() - m_keyboard->height() - 10);
// }
// else{
// widgetGlobalPos = widgetGlobalPos + QPoint(0,30);
// }
// m_keyboard->move(widgetGlobalPos);
//改為以下代碼,增加考慮擴(kuò)展屏幕的代碼
//獲取當(dāng)前屏幕及屏幕幾何矩形坐標(biāo)
int currentScreen = qApp->desktop()->screenNumber(widgetTmp);
if (currentScreen < 0)
{
currentScreen = 0;
}
QScreen *screen = QGuiApplication::screens().at(currentScreen);
QRect rect = screen->geometry();
QPoint widgetGlobalPos = widgetTmp->mapToGlobal(QPoint(0, 0));
if(widgetGlobalPos.x() < rect.x()) {
widgetGlobalPos.setX(rect.x());
}
if (widgetGlobalPos.y() < rect.y())
{
widgetGlobalPos.setY(rect.y());
}
if(rect.width() - widgetGlobalPos.x() < m_keyboard->width()) {
widgetGlobalPos.setX(rect.width() - m_keyboard->width());
}
if(rect.height() - widgetGlobalPos.y() - 30 < m_keyboard->height()) {
widgetGlobalPos.setY(widgetGlobalPos.y() - m_keyboard->height() - 10);
}
else {
widgetGlobalPos = widgetGlobalPos + QPoint(0,30);
}
m_keyboard->move(widgetGlobalPos);
}
}
3、修改后重新編譯、重新部署,再運(yùn)行demo程序就正常了。
6、修改QtInputMethod_GooglePinyin源碼解決在Linux Arm環(huán)境下部分應(yīng)用資源沖突導(dǎo)致無法加載qss和ota字體的問題
1、當(dāng)在嵌入式Linu Arm環(huán)境中使用GooglePinyin虛擬鍵盤時(shí),如果某個(gè)Qt應(yīng)用使用了資源文件可能會(huì)導(dǎo)致GooglePinyin虛擬鍵盤無法加載:/res/stylesheet.qss和:res/FontAwesome.otf從而無法顯示虛擬鍵盤進(jìn)而導(dǎo)致程序崩潰。
2、要解決這個(gè)問題,需要修改plugin項(xiàng)目中的keyboardform.cpp文件中的KeyboardForm::KeyboardForm(QWidget *parent)
: QWidget(parent)中的代碼,如下:文章來源:http://www.zghlxwxcb.cn/news/detail-656764.html
KeyboardForm::KeyboardForm(QWidget *parent)
: QWidget(parent)
{
character_btns_list.clear();
current_mode = InputMode::en;
upper_mode = false;
m_symbol_page = 0;
this->setFixedSize(800,250);
int keyboard_btn_width = this->width()/11.5;
int keyboard_btn_height = this->height()/5.0;
//設(shè)置主窗體樣式
this->setAttribute(Qt::WA_TranslucentBackground);
this->setWindowFlags(Qt::Tool | \
Qt::FramelessWindowHint | \
Qt::WindowStaysOnTopHint | \
Qt::WindowDoesNotAcceptFocus);
//加載QSS樣式表
// QFile qss(":/res/stylesheet.qss");
// if(false == qss.open(QFile::ReadOnly))return;
// this->setStyleSheet(qss.readAll());
// qss.close();
//以上代碼在窗體及容器控件的多層嵌套情況下會(huì)導(dǎo)致qss樣式資源無法加載,改為以下代碼
QFile qss(":/res/stylesheet.qss");
if (qss.exists() && qss.open(QFile::ReadOnly))
{
this->setStyleSheet(qss.readAll());
qss.close();
}
else
{
QString strcss = "" \
"QWidget{background-color:black;}" \
"QPushButton{font:25px;background-color:gray;color:white;border:2px solid black;border-radius:10px;}" \
"QPushButton#function_button{background-color:rgb(80,80,80);color:rgb(200,200,200);font:20px;}" \
"QPushButton:hover{background-color:green;color:white;}" \
"QPushButton:pressed,QPushButton#function_button::pressed{background-color:red;color:white;}" \
"QPushButton#function_button:checked{background-color:rgb(200,100,50);color:yellow;}" \
"QPushButton#hanzichangepage,QPushButton#hanzicandidates{background-color:transparent;color:white;}" \
"QPushButton#hanzichangepage:pressed{color:red;}" \
"QPushButton#hanzichangepage:disabled{color:gray;}" \
"QPushButton#emoji{background-color:rgb(80,80,80);color:yellow;}" \
"QPushButton#emoji:hover{color:red;}" \
"QPushButton#emoji:pressed{color:blue;}" \
"QLabel{background-color:rgba(255,255,255,80);border-radius:2px;color:white;font:15px;margin-left: 5px;}";
this->setStyleSheet(strcss);
}
//圖標(biāo)字體
// int fontId = QFontDatabase::addApplicationFont(":/res/FontAwesome.otf");
// QString fontName = QFontDatabase::applicationFontFamilies(fontId).at(0);
// QFont btnicofont(fontName);
// btnicofont.setPixelSize(10);
//以上代碼在窗體及容器控件的多層嵌套情況下會(huì)導(dǎo)致字體資源無法加載,改為以下方式
QFont btnicofont(QString("Arial"));
btnicofont.setPixelSize(10);
if (QFile::exists(":res/FontAwesome.otf"))
{
int fontId = QFontDatabase::addApplicationFont(":/res/FontAwesome.otf");
QString fontName = QFontDatabase::applicationFontFamilies(fontId).at(0);
btnicofont.setFamily(fontName);
}
#ifdef Q_OS_LINUX
else if (QFile::exists("/usr/share/font-awesome/fonts/FontAwesome.otf"))
{
int fontId = QFontDatabase::addApplicationFont("/usr/share/font-awesome/fonts/FontAwesome.otf");
QString fontName = QFontDatabase::applicationFontFamilies(fontId).at(0);
btnicofont.setFamily(fontName);
}
#endif
else
{
qWarning() << "can not find FontAwesome.otf";
}
//單行布局
QHBoxLayout *hb[6];
for(int i=0; i<6; i++)
{
hb[i] = new QHBoxLayout();
hb[i]->setMargin(0);
i == 1 ? hb[i]->setSpacing(2) : hb[i]->setSpacing(0);
}
widget_pinyin = new QWidget(this);
widget_pinyin->setFixedHeight(keyboard_btn_height);
//拼音緩存
m_label_pinyin = new QLabel(this);
m_label_pinyin->setFixedHeight(keyboard_btn_height*0.4);
hb[0]->addWidget(m_label_pinyin);
hb[0]->addStretch(1);
//漢子緩存
for(int i=0; i<chinesecharacters_number; i++)
{
QPushButton *btn = new QPushButton(this);
btn->setFixedHeight(keyboard_btn_height*0.6);
hb[1]->addWidget(btn);
if(i != chinesecharacters_number - 1) hb[1]->addStretch(1);
if (i == 0 || i == chinesecharacters_number-1)
{
change_chinese_characters_page_list.append(btn);
btn->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
btn->setFont(btnicofont);
btn->setText(i == 0 ? QString(QChar(0xf0d9)) : QString(QChar(0xf0da)));
btn->setObjectName("hanzichangepage");
i == 0 ? \
connect(btn, &QPushButton::clicked, this, &KeyboardForm::chineseCharactersUpdatePrevious) :
connect(btn, &QPushButton::clicked, this, &KeyboardForm::chineseCharactersUpdateNext);
}
else
{
chinese_characters_list.append(btn);
btn->setObjectName("hanzicandidates");
connect(btn, &QPushButton::clicked, this, &KeyboardForm::chineseCharactersSelected);
}
}
QVBoxLayout *vb_pinyin = new QVBoxLayout(widget_pinyin);
vb_pinyin->addLayout(hb[0]);
vb_pinyin->addLayout(hb[1]);
vb_pinyin->setMargin(0);
vb_pinyin->setSpacing(0);
widget_keyboard = new QWidget(this);
widget_keyboard->setFixedHeight(keyboard_btn_height*4.0);
//鍵盤
for(int i=0; i<29; i++)
{
QPushButton *btn = new QPushButton(QChar(keyboard_characters[i]),this);
btn->setFixedSize(keyboard_btn_width, keyboard_btn_height);
character_btns_list.append(btn);
connect(btn, &QPushButton::clicked, this, &KeyboardForm::characterButtonClicked);
}
//第一排字母:0-9
for(int i=0; i<10; i++)
{
hb[2]->addWidget(character_btns_list.at(i));
}
QPushButton *btn_backspace = new QPushButton(QChar(0xf060));
btn_backspace->setFont(btnicofont);
btn_backspace->setFixedSize(keyboard_btn_width*1.5, keyboard_btn_height);
btn_backspace->setObjectName("function_button");
hb[2]->addWidget(btn_backspace);
connect(btn_backspace, &QPushButton::clicked, this, &KeyboardForm::btnBackspaceClicked);
//第二排字母:10-18
hb[3]->addStretch(1);
for(int i=10; i<19; i++)
{
hb[3]->addWidget(character_btns_list.at(i));
}
QPushButton *btn_enter = new QPushButton("Enter");
btn_enter->setFixedSize(keyboard_btn_width*1.5, keyboard_btn_height);
btn_enter->setObjectName("function_button");
hb[3]->addWidget(btn_enter);
hb[3]->addStretch(1);
connect(btn_enter, &QPushButton::clicked, this, &KeyboardForm::btnEnterClicked);
//第三排字母:20-26
QPushButton *btn_upper = new QPushButton(QChar(0xf062));
btn_upper->setFixedSize(keyboard_btn_width*1.5, keyboard_btn_height);
btn_upper->setFont(btnicofont);
btn_upper->setObjectName("function_button");
hb[4]->addWidget(btn_upper);
connect(btn_upper, &QPushButton::clicked, this, &KeyboardForm::btnUpperClicked);
for(int i=19; i<29; i++)
{
hb[4]->addWidget(character_btns_list.at(i));
}
character_btns_list.append(btn_upper);
//第四排功能鍵
QPushButton *btn_symbols = new QPushButton(".?123");
btn_symbols->setFixedSize(keyboard_btn_width*1.5, keyboard_btn_height);
btn_symbols->setObjectName("function_button");
hb[5]->addWidget(btn_symbols);
connect(btn_symbols, &QPushButton::clicked, this, &KeyboardForm::btnSymbolsClicked);
QPushButton *btn_language = new QPushButton(QChar(0xf0ac));
btn_language->setFixedSize(keyboard_btn_width, keyboard_btn_height);
btn_language->setFont(btnicofont);
btn_language->setObjectName("function_button");
hb[5]->addWidget(btn_language);
connect(btn_language, &QPushButton::clicked, this, &KeyboardForm::btnLanguageClicked);
QPushButton *btn_blankspace = new QPushButton("English");
btn_blankspace->setFixedHeight(keyboard_btn_height);
hb[5]->addWidget(btn_blankspace);
character_btns_list.append(btn_blankspace);
connect(btn_blankspace, &QPushButton::clicked, this, &KeyboardForm::btnBlankspaceClicked);
QPushButton *btn_emoji = new QPushButton(QChar(0xf118));
btn_emoji->setFixedSize(keyboard_btn_width, keyboard_btn_height);
btn_emoji->setFont(btnicofont);
btn_emoji->setObjectName("emoji");
hb[5]->addWidget(btn_emoji);
connect(btn_emoji, &QPushButton::clicked, this, &KeyboardForm::btnEmojiClicked);
QPushButton *btn_hidekeyboard = new QPushButton(QString(QChar(0xf11c)).append(QChar(0xf103)));
btn_hidekeyboard->setFixedSize(keyboard_btn_width*1.5, keyboard_btn_height);
btn_hidekeyboard->setFont(btnicofont);
btn_hidekeyboard->setObjectName("function_button");
hb[5]->addWidget(btn_hidekeyboard);
connect(btn_hidekeyboard, &QPushButton::clicked, this, &KeyboardForm::hideKeyboard);
QVBoxLayout *vb_keyboard = new QVBoxLayout(widget_keyboard);
vb_keyboard->setMargin(0);
vb_keyboard->setSpacing(0);
for(int i=2; i<6; i++)
{
vb_keyboard->addLayout(hb[i]);
}
QVBoxLayout *vb_system = new QVBoxLayout(this);
vb_system->setMargin(0);
vb_system->setSpacing(0);
vb_system->addStretch(1);
vb_system->addWidget(widget_pinyin);
vb_system->addWidget(widget_keyboard);
widget_pinyin->hide();
updateButtonStateOfChineseCharacters();
}
7、完整源碼下載
修改后的完整源碼下載地址文章來源地址http://www.zghlxwxcb.cn/news/detail-656764.html
到了這里,關(guān)于QT學(xué)習(xí)筆記-QT5.15編譯及安裝谷歌拼音輸入法(QtInputMethod_GooglePinyin)的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!