使用Qt中实现中英文键盘,支持各种linux嵌入式设备。
实现思路:需要一个中文字体库,将字体库加载到一个Hash容器,字母和拼音作为key值,对应的中文作为value值。
核心代码:
#include "UKeyBoard.h"
#include "ui_UKeyBoard.h"
#include <QFile>
#include <QKeyEvent>
#include <QDebug>
UKeyBoard::UKeyBoard(QWidget *parent) :
QWidget(parent),
ui(new Ui::UKeyBoard)
{
ui->setupUi(this);
QFile* file = new QFile(QApplication::applicationDirPath()+"/PinYin_Chinese.txt");
file->open(QIODevice::ReadOnly);
QString sss = QString::fromUtf8(file->readAll());
qDebug()<<sss.size();
QStringList ssslist = sss.split("\r\n");
foreach (QString var, ssslist) {
QStringList pinyin = var.mid(1,var.size() - 1).split(" ");
QString word = var.mid(0,1);
for (int i = 0; i < pinyin.size(); ++i) {
if (m_wordHash.find(pinyin[i]) == m_wordHash.end())
m_wordHash.insert(pinyin[i],QStringList()<<word);
else
m_wordHash[pinyin[i]].append(word);
}
}
connectInit();
}
UKeyBoard::~UKeyBoard()
{
delete ui;
}
void UKeyBoard::connectInit()
{
connect(ui->Key_Q_Btn,&QToolButton::clicked,this,&UKeyBoard::slotKeyBtn);
connect(ui->Key_W_Btn,&QToolButton::clicked,this,&UKeyBoard::slotKeyBtn);
connect(ui->Key_E_Btn,&QToolButton::clicked,this,&UKeyBoard::slotKeyBtn);
connect(ui->Key_R_Btn,&QToolButton::clicked,this,&UKeyBoard::slotKeyBtn);
connect(ui->Key_T_Btn,&QToolButton::clicked,this,&UKeyBoard::slotKeyBtn);
connect(ui->Key_Y_Btn,&QToolButton::clicked,this,&UKeyBoard::slotKeyBtn);
connect(ui->Key_U_Btn,&QToolButton::clicked,this,&UKeyBoard::slotKeyBtn);
connect(ui->Key_I_Btn,&QToolButton::clicked,this,&UKeyBoard::slotKeyBtn);
connect(ui->Key_O_Btn,&QToolButton::clicked,this,&UKeyBoard::slotKeyBtn);
connect(ui->Key_P_Btn,&QToolButton::clicked,this,&UKeyBoard::slotKeyBtn);
connect(ui->Key_A_Btn,&QToolButton::clicked,this,&UKeyBoard::slotKeyBtn);
connect(ui->Key_S_Btn,&QToolButton::clicked,this,&UKeyBoard::slotKeyBtn);
connect(ui->Key_D_Btn,&QToolButton::clicked,this,&UKeyBoard::slotKeyBtn);
connect(ui->Key_F_Btn,&QToolButton::clicked,this,&UKeyBoard::slotKeyBtn);
connect(ui->Key_G_Btn,&QToolButton::clicked,this,&UKeyBoard::slotKeyBtn);
connect(ui->Key_H_Btn,&QToolButton::clicked,this,&UKeyBoard::slotKeyBtn);
connect(ui->Key_J_Btn,&QToolButton::clicked,this,&UKeyBoard::slotKeyBtn);
connect(ui->Key_K_Btn,&QToolButton::clicked,this,&UKeyBoard::slotKeyBtn);
connect(ui->Key_L_Btn,&QToolButton::clicked,this,&UKeyBoard::slotKeyBtn);
connect(ui->Key_Z_Btn,&QToolButton::clicked,this,&UKeyBoard::slotKeyBtn);
connect(ui->Key_X_Btn,&QToolButton::clicked,this,&UKeyBoard::slotKeyBtn);
connect(ui->Key_C_Btn,&QToolButton::clicked,this,&UKeyBoard::slotKeyBtn);
connect(ui->Key_V_Btn,&QToolButton::clicked,this,&UKeyBoard::slotKeyBtn);
connect(ui->Key_B_Btn,&QToolButton::clicked,this,&UKeyBoard::slotKeyBtn);
connect(ui->Key_N_Btn,&QToolButton::clicked,this,&UKeyBoard::slotKeyBtn);
connect(ui->Key_M_Btn,&QToolButton::clicked,this,&UKeyBoard::slotKeyBtn);
connect(ui->wordBtn1,&QToolButton::clicked,this,&UKeyBoard::slotInputChange);
connect(ui->wordBtn2,&QToolButton::clicked,this,&UKeyBoard::slotInputChange);
connect(ui->wordBtn3,&QToolButton::clicked,this,&UKeyBoard::slotInputChange);
connect(ui->wordBtn4,&QToolButton::clicked,this,&UKeyBoard::slotInputChange);
connect(ui->wordBtn5,&QToolButton::clicked,this,&UKeyBoard::slotInputChange);
connect(ui->wordBtn6,&QToolButton::clicked,this,&UKeyBoard::slotInputChange);
connect(ui->wordBtn7,&QToolButton::clicked,this,&UKeyBoard::slotInputChange);
}
//输入中文
void UKeyBoard::slotInputChange()
{
QToolButton* btn = (QToolButton*)sender();
QString value = btn->text();
QKeyEvent *key = new QKeyEvent(QEvent::KeyPress, 'w', Qt::NoModifier, value.split(" ").last());
//通过postEvent将事件key的文本内容发送出去,QApplication::focusWidget()当前窗口中,有光标的控件上
QApplication::postEvent(ui->inputEdit->focusWidget(), key); //_currentLineEdit
m_curWords.clear();
ui->selectWordEdit->setText("");
ui->selectWordEdit->hide();
ui->wordBtn1->setText("");
ui->wordBtn2->setText("");
ui->wordBtn3->setText("");
ui->wordBtn4->setText("");
ui->wordBtn5->setText("");
ui->wordBtn6->setText("");
ui->wordBtn7->setText("");
}
//上一页中文
void UKeyBoard::on_lastPageBtn_clicked()
{
if (_index > 0)
_index--;
QString val = ui->selectWordEdit->text();
wordChange(val,_index);
}
//下一页中文
void UKeyBoard::on_nextPageBtn_clicked()
{
_index++;
QString val = ui->selectWordEdit->text();
if (wordChange(val,_index) < 0)
_index--;
}
//按键输入
void UKeyBoard::slotKeyBtn()
{
QToolButton* btn = (QToolButton*)sender();
ui->inputEdit->setFocus();
if (_keyType == 0)
{
QString val = ui->selectWordEdit->text();
int position = ui->selectWordEdit->cursorPosition();
val.insert(position,btn->text());
ui->selectWordEdit->setText(val);
m_curWords.clear();
m_curWords.append(m_wordHash.value(val));
for (QHash<QString,QStringList>::const_iterator it = m_wordHash.begin(); it != m_wordHash.end();it++) {
if (it.key().indexOf(val) == 0)
{
if (it.key() == val)
continue;
m_curWords.append(it.value());
}
}
wordChange(val,0);
updateSelectInputPos();
}
else
{
QString value = btn->text();
QKeyEvent *key = new QKeyEvent(QEvent::KeyPress, 'w', Qt::NoModifier, value);
//通过postEvent将事件key的文本内容发送出去,QApplication::focusWidget()当前窗口中,有光标的控件上
QApplication::postEvent(ui->inputEdit->focusWidget(), key); //_currentLineEdit
}
}
//更新编辑框大小和状态
void UKeyBoard::updateSelectInputPos()
{
if (!ui->selectWordEdit->text().isEmpty())
{
ui->selectWordEdit->setFocus();
ui->selectWordEdit->show();
QFontMetrics _metrics(ui->inputEdit->font());
QFontMetrics _metrics1(ui->selectWordEdit->font());
QString inputVal = ui->inputEdit->text().mid(0,ui->inputEdit->cursorPosition());
int inputPos = 5+_metrics.width(inputVal)+ui->inputEdit->x();
ui->selectWordEdit->setGeometry(inputPos,ui->inputEdit->y()+3,_metrics1.width(ui->selectWordEdit->text())+6,36);
}
else
{
ui->inputEdit->setFocus();
ui->selectWordEdit->clear();
ui->selectWordEdit->hide();
}
}
//中文检索
int UKeyBoard::wordChange(QString val,int index)
{
_index = index;
if (m_curWords.size() < 7*index)
return -1;
ui->wordBtn1->setText("");
ui->wordBtn2->setText("");
ui->wordBtn3->setText("");
ui->wordBtn4->setText("");
ui->wordBtn5->setText("");
ui->wordBtn6->setText("");
ui->wordBtn7->setText("");
if (m_curWords.size()>7*index)
ui->wordBtn1->setText("1 "+m_curWords[7*index]);
if (m_curWords.size()>7*index+1)
ui->wordBtn2->setText("2 "+m_curWords[7*index+1]);
if (m_curWords.size()>7*index+2)
ui->wordBtn3->setText("3 "+m_curWords[7*index+2]);
if (m_curWords.size()>7*index+3)
ui->wordBtn4->setText("4 "+m_curWords[7*index+3]);
if (m_curWords.size()>7*index+4)
ui->wordBtn5->setText("5 "+m_curWords[7*index+4]);
if (m_curWords.size()>7*index+5)
ui->wordBtn6->setText("6 "+m_curWords[7*index+5]);
if (m_curWords.size()>7*index+6)
ui->wordBtn7->setText("7 "+m_curWords[7*index+6]);
return 0;
}
//大小写切换
void UKeyBoard::on_capsBtn_clicked()
{
if (!_capsType)
_capsType = true;
else
_capsType = false;
if (_keyType == 1)
{
if (_capsType)
{
changeKeyType(2);
}
else
{
changeKeyType(1);
}
}
else if (_keyType == 2)
{
if (_capsType)
{
changeKeyType(4);
}
else
{
changeKeyType(3);
}
}
else if (_keyType == 0)
{
QString val = ui->inputEdit->text();
int position = ui->inputEdit->cursorPosition();
QString wordVal = ui->wordBtn1->text().split(" ").last();
if (wordVal.isEmpty())
wordVal = ui->selectWordEdit->text();
val.insert(position,wordVal+",");
ui->inputEdit->setText(val);
ui->inputEdit->setCursorPosition(position+wordVal.size()+1);
}
ui->inputEdit->setFocus();
ui->selectWordEdit->clear();
ui->selectWordEdit->hide();
ui->wordBtn1->setText("");
ui->wordBtn2->setText("");
ui->wordBtn3->setText("");
ui->wordBtn4->setText("");
ui->wordBtn5->setText("");
ui->wordBtn6->setText("");
ui->wordBtn7->setText("");
}
//删除字符
void UKeyBoard::on_delBtn_clicked()
{
QString val = ui->selectWordEdit->text();
if (!val.isEmpty())
{
int cursorPosition = ui->selectWordEdit->cursorPosition()-1;
val.remove(cursorPosition,1);
ui->selectWordEdit->setText(val);
ui->selectWordEdit->setCursorPosition(cursorPosition);
m_curWords.clear();
m_curWords.append(m_wordHash.value(val));
for (QHash<QString,QStringList>::const_iterator it = m_wordHash.begin(); it != m_wordHash.end();it++) {
if (it.key().indexOf(val) == 0)
{
if (it.key() == val)
continue;
m_curWords.append(it.value());
}
}
wordChange(val,0);
if (val.isEmpty())
{
ui->wordBtn1->setText("");
ui->wordBtn2->setText("");
ui->wordBtn3->setText("");
ui->wordBtn4->setText("");
ui->wordBtn5->setText("");
ui->wordBtn6->setText("");
ui->wordBtn7->setText("");
ui->selectWordEdit->hide();
ui->inputEdit->setFocus();
}
}
else
{
val = ui->inputEdit->text();
int cursorPosition = ui->inputEdit->cursorPosition()-1;
val.remove(cursorPosition,1);
ui->inputEdit->setText(val);
ui->inputEdit->setCursorPosition(cursorPosition);
}
updateSelectInputPos();
}
//切换键盘类型
void UKeyBoard::on_swithBtn_clicked()
{
if (_keyType == 0)
_keyType = 1;
else if (_keyType == 1)
_keyType = 2;
else if (_keyType == 2)
_keyType = 0;
_capsType = false;
if (_keyType == 0)
{
changeKeyType(0);
ui->swithBtn->setText("EN");
}
else if (_keyType == 1)
{
changeKeyType(1);
ui->swithBtn->setText("123");
}
else
{
changeKeyType(3);
ui->swithBtn->setText("CN");
}
ui->inputEdit->setFocus();
ui->selectWordEdit->clear();
ui->selectWordEdit->hide();
}
//空格
void UKeyBoard::on_spaceBtn_clicked()
{
int position = ui->inputEdit->cursorPosition();
QString val = ui->inputEdit->text();
if (_keyType == 0)
{
QString wordVal = ui->wordBtn1->text().split(" ").last();
if (wordVal.isEmpty())
wordVal = ui->selectWordEdit->text();
val.insert(position,wordVal+" ");
ui->inputEdit->setText(val);
ui->inputEdit->setCursorPosition(position+wordVal.size()+1);
}
else
{
val.insert(position," ");
ui->inputEdit->setText(val);
ui->inputEdit->setCursorPosition(position+1);
}
ui->inputEdit->setFocus();
ui->selectWordEdit->clear();
ui->selectWordEdit->hide();
ui->wordBtn1->setText("");
ui->wordBtn2->setText("");
ui->wordBtn3->setText("");
ui->wordBtn4->setText("");
ui->wordBtn5->setText("");
ui->wordBtn6->setText("");
ui->wordBtn7->setText("");
}
//光标左移
void UKeyBoard::on_leftMoveBtn_clicked()
{
if (ui->selectWordEdit->text().isEmpty())
{
if (ui->inputEdit->cursorPosition()-1 < 0)
ui->inputEdit->setCursorPosition(ui->inputEdit->text().size());
else
ui->inputEdit->setCursorPosition(ui->inputEdit->cursorPosition()-1);
}
else
{
if (ui->selectWordEdit->cursorPosition()-1 < 0)
ui->selectWordEdit->setCursorPosition(ui->selectWordEdit->text().size());
else
ui->selectWordEdit->setCursorPosition(ui->selectWordEdit->cursorPosition()-1);
}
}
//光标右移
void UKeyBoard::on_rightMoveBtn_clicked()
{
if (ui->selectWordEdit->text().isEmpty())
{
if (ui->inputEdit->cursorPosition()+1 > ui->inputEdit->text().size())
ui->inputEdit->setCursorPosition(0);
else
ui->inputEdit->setCursorPosition(ui->inputEdit->cursorPosition()+1);
}
else
{
if (ui->selectWordEdit->cursorPosition()+1 > ui->selectWordEdit->text().size())
ui->selectWordEdit->setCursorPosition(0);
else
ui->selectWordEdit->setCursorPosition(ui->selectWordEdit->cursorPosition()+1);
}
}
void UKeyBoard::on_enterBtn_clicked()
{
this->hide();
}
//切换键盘类型
void UKeyBoard::changeKeyType(int type)
{
ui->selectWordEdit->setText("");
ui->wordBtn1->setText("");
ui->wordBtn2->setText("");
ui->wordBtn3->setText("");
ui->wordBtn4->setText("");
ui->wordBtn5->setText("");
ui->wordBtn6->setText("");
ui->wordBtn7->setText("");
if (type == 0)
{
_capsType = false;
ui->Key_A_Btn->setText("a");
ui->Key_S_Btn->setText("s");
ui->Key_D_Btn->setText("d");
ui->Key_F_Btn->setText("f");
ui->Key_G_Btn->setText("g");
ui->Key_H_Btn->setText("h");
ui->Key_J_Btn->setText("j");
ui->Key_K_Btn->setText("k");
ui->Key_L_Btn->setText("l");
ui->Key_Q_Btn->setText("q");
ui->Key_W_Btn->setText("w");
ui->Key_E_Btn->setText("e");
ui->Key_R_Btn->setText("r");
ui->Key_T_Btn->setText("t");
ui->Key_Y_Btn->setText("y");
ui->Key_U_Btn->setText("u");
ui->Key_I_Btn->setText("i");
ui->Key_O_Btn->setText("o");
ui->Key_P_Btn->setText("p");
ui->Key_Z_Btn->setText("z");
ui->Key_X_Btn->setText("x");
ui->Key_C_Btn->setText("c");
ui->Key_V_Btn->setText("v");
ui->Key_B_Btn->setText("b");
ui->Key_N_Btn->setText("n");
ui->Key_M_Btn->setText("m");
ui->capsBtn->setIcon(QIcon());
ui->capsBtn->setText(".");
}
else if (type == 1)
{
_capsType = false;
ui->Key_A_Btn->setText("a");
ui->Key_S_Btn->setText("s");
ui->Key_D_Btn->setText("d");
ui->Key_F_Btn->setText("f");
ui->Key_G_Btn->setText("g");
ui->Key_H_Btn->setText("h");
ui->Key_J_Btn->setText("j");
ui->Key_K_Btn->setText("k");
ui->Key_L_Btn->setText("l");
ui->Key_Q_Btn->setText("q");
ui->Key_W_Btn->setText("w");
ui->Key_E_Btn->setText("e");
ui->Key_R_Btn->setText("r");
ui->Key_T_Btn->setText("t");
ui->Key_Y_Btn->setText("y");
ui->Key_U_Btn->setText("u");
ui->Key_I_Btn->setText("i");
ui->Key_O_Btn->setText("o");
ui->Key_P_Btn->setText("p");
ui->Key_Z_Btn->setText("z");
ui->Key_X_Btn->setText("x");
ui->Key_C_Btn->setText("c");
ui->Key_V_Btn->setText("v");
ui->Key_B_Btn->setText("b");
ui->Key_N_Btn->setText("n");
ui->Key_M_Btn->setText("m");
ui->capsBtn->setIcon(QIcon(":/img/keyboard-up_1.png"));
ui->capsBtn->setIconSize(QSize(28,33));
ui->capsBtn->setText("");
}
else if (type == 2)
{
ui->Key_A_Btn->setText("A");
ui->Key_S_Btn->setText("S");
ui->Key_D_Btn->setText("D");
ui->Key_F_Btn->setText("F");
ui->Key_G_Btn->setText("G");
ui->Key_H_Btn->setText("H");
ui->Key_J_Btn->setText("J");
ui->Key_K_Btn->setText("K");
ui->Key_L_Btn->setText("L");
ui->Key_Q_Btn->setText("Q");
ui->Key_W_Btn->setText("W");
ui->Key_E_Btn->setText("E");
ui->Key_R_Btn->setText("R");
ui->Key_T_Btn->setText("T");
ui->Key_Y_Btn->setText("Y");
ui->Key_U_Btn->setText("U");
ui->Key_I_Btn->setText("I");
ui->Key_O_Btn->setText("O");
ui->Key_P_Btn->setText("P");
ui->Key_Z_Btn->setText("Z");
ui->Key_X_Btn->setText("X");
ui->Key_C_Btn->setText("C");
ui->Key_V_Btn->setText("V");
ui->Key_B_Btn->setText("B");
ui->Key_N_Btn->setText("N");
ui->Key_M_Btn->setText("M");
ui->capsBtn->setIcon(QIcon(":/img/keyboard-up.png"));
ui->capsBtn->setIconSize(QSize(28,33));
ui->capsBtn->setText("");
}
else if (type == 3)
{
ui->Key_A_Btn->setText("-");
ui->Key_S_Btn->setText("+");
ui->Key_D_Btn->setText("=");
ui->Key_F_Btn->setText("/");
ui->Key_G_Btn->setText("\\");
ui->Key_H_Btn->setText(";");
ui->Key_J_Btn->setText("`");
ui->Key_K_Btn->setText(",");
ui->Key_L_Btn->setText(".");
ui->Key_Q_Btn->setText("1");
ui->Key_W_Btn->setText("2");
ui->Key_E_Btn->setText("3");
ui->Key_R_Btn->setText("4");
ui->Key_T_Btn->setText("5");
ui->Key_Y_Btn->setText("6");
ui->Key_U_Btn->setText("7");
ui->Key_I_Btn->setText("8");
ui->Key_O_Btn->setText("9");
ui->Key_P_Btn->setText("0");
ui->Key_Z_Btn->setText("[");
ui->Key_X_Btn->setText("]");
ui->Key_C_Btn->setText("‘");
ui->Key_V_Btn->setText("...");
ui->Key_B_Btn->setText("¥");
ui->Key_N_Btn->setText("。");
ui->Key_M_Btn->setText("、");
ui->capsBtn->setIcon(QIcon());
ui->capsBtn->setText("#+=");
}
else if (type == 4)
{
ui->Key_A_Btn->setText("!");
ui->Key_S_Btn->setText("@");
ui->Key_D_Btn->setText("#");
ui->Key_F_Btn->setText("$");
ui->Key_G_Btn->setText("%");
ui->Key_H_Btn->setText("^");
ui->Key_J_Btn->setText("&");
ui->Key_K_Btn->setText("*");
ui->Key_L_Btn->setText("(");
ui->Key_Q_Btn->setText(")");
ui->Key_W_Btn->setText("_");
ui->Key_E_Btn->setText("°");
ui->Key_R_Btn->setText("{");
ui->Key_T_Btn->setText("}");
ui->Key_Y_Btn->setText("|");
ui->Key_U_Btn->setText(":");
ui->Key_I_Btn->setText("\"");
ui->Key_O_Btn->setText("<");
ui->Key_P_Btn->setText(">");
ui->Key_Z_Btn->setText("?");
ui->Key_X_Btn->setText("~");
ui->Key_C_Btn->setText("”");
ui->Key_V_Btn->setText("“");
ui->Key_B_Btn->setText("《");
ui->Key_N_Btn->setText("》");
ui->Key_M_Btn->setText("—");
ui->capsBtn->setIcon(QIcon());
ui->capsBtn->setText("123");
}
}
完整源码