Qt 软键盘(虚拟键盘) 可直接编译,嵌入式使用
立即下载
资源介绍:
软键盘在Qt工程中已经封装为子工程,demo工程可以直接跑通,亲测。
包括inputnew.h里面有每个函数的注解,适合Qt嵌入式的开发者学习使用。键盘是自适应的
#pragma execution_character_set("utf-8")
#include "frminputnew.h"
#include "ui_frminputnew.h"
frmInputNew *frmInputNew::self = NULL;
frmInputNew *frmInputNew::Instance()
{
if (!self)
{
static QMutex mutex;
QMutexLocker locker(&mutex);
if (!self)
{
self = new frmInputNew;
}
}
return self;
}
frmInputNew::frmInputNew(QWidget *parent) : QWidget(parent), ui(new Ui::frmInputNew)
{
//修改1:只有数字键盘和字母键盘,数字键盘大小(320x320),字母键盘大小(500x320)
//widgetMain控件改成stackedWidgetMain,添加字母键盘UI和数字键盘UI
//修改2:widgetTop控件隐藏
ui->setupUi(this);
this->initProperty();
this->initForm();
QTimer::singleShot(1000, this, SLOT(initDb()));
}
frmInputNew::~frmInputNew()
{
timerPress->stop();
delete ui;
}
void frmInputNew::mouseMoveEvent(QMouseEvent *e)
{
if (mousePressed && (e->buttons() && Qt::LeftButton)) {
this->move(e->globalPos() - mousePoint);
e->accept();
}
}
void frmInputNew::mousePressEvent(QMouseEvent *e)
{
if (e->button() == Qt::RightButton) {
mousePressed = true;
mousePoint = e->globalPos() - this->pos();
e->accept();
}
}
void frmInputNew::mouseReleaseEvent(QMouseEvent *)
{
mousePressed = false;
}
//事件过滤器,用于识别鼠标单击汉字标签处获取对应汉字
bool frmInputNew::eventFilter(QObject *obj, QEvent *event)
{
if (event->type() == QEvent::MouseButtonPress)
{
QMouseEvent *mouseEvent = static_cast(event);
if (mouseEvent->button() == Qt::LeftButton)
{
if (obj == ui->labCh1)
{
setChinese(0);
}
else if (obj == ui->labCh2)
{
setChinese(1);
}
else if (obj == ui->labCh3)
{
setChinese(2);
}
else if (obj == ui->labCh4)
{
setChinese(3);
}
else if (obj == ui->labCh5)
{
setChinese(4);
}
else if (currentEditType != "" && obj != ui->btnClose)
{
QString objName = obj->objectName();
//qDebug()<<"objName:"<parent() != 0x0 && !obj->property("noinput").toBool() && objName != "frmMainWindow"
&& objName != "frmInputWindow" && objName != "qt_edit_menu" && objName != "labPY" && objName != "stackedWidgetMain")
{
if (obj->inherits("QGroupBox") || obj->inherits("QFrame") || obj->inherits("QMenu"))
{
this->hide();
}
else
{
showPanel();
}
}
}
btnPress = (QPushButton *)obj;
if (checkPress())
{
isPress = true;
timerPress->start(500);
}
return false;
}
}
else if (event->type() == QEvent::MouseButtonRelease)
{
btnPress = (QPushButton *)obj;
if (checkPress())
{
btnPress->setAttribute(Qt::WA_UnderMouse, false);
QHoverEvent hoverEvent(QEvent::HoverLeave, QPoint(4, 4), QPoint(0, 0));
QApplication::sendEvent(btnPress, &hoverEvent);
isPress = false;
timerPress->stop();
}
return false;
}
else if (event->type() == QEvent::KeyPress)
{
//如果输入法窗体不可见,则不需要处理
if (!isVisible() && !mini)
{
return QWidget::eventFilter(obj, event);
}
QString labText = ui->labPY->text();
QKeyEvent *keyEvent = static_cast(event);
//Shift切换输入法模式,esc键关闭输入法面板,空格取第一个汉字,退格键删除
//中文模式下回车键取拼音,中文模式下当没有拼音时可以输入空格
if (keyEvent->key() == Qt::Key_Space)
{
if (labText != "")
{
ui->labPY->setText("");
setChinese(0);
return true;
}
else
{
return false;
}
}
else if (keyEvent->key() == Qt::Key_Return || keyEvent->key() == Qt::Key_Enter)
{
if (labText != "")
{
insertValue(labText);
ui->labPY->setText("");
selectChinese();
}
else
{
this->setVisible(false);
}
return true;
}
else if (keyEvent->key() == Qt::Key_Control)
{
if (mini)
{
ui->btnType->click();
return true;
}
}
else if (keyEvent->key() == Qt::Key_Shift)
{
if (!mini)
{
ui->btnType->click();
return true;
}
}
else if (keyEvent->key() == Qt::Key_Escape)
{
ui->btnClose->click();
return true;
}
else if (keyEvent->key() == Qt::Key_Backspace)
{
if (inputType == "chinese")
{
QString txt = labText;
int len = txt.length();
if (len > 0)
{
ui->labPY->setText(txt.left(len - 1));
selectChinese();
return true;
}
}
}
else if (keyEvent->key() == Qt::Key_CapsLock)
{
ui->btnUpper->click();
return true;
}
else if (keyEvent->text() == "+" || keyEvent->text() == "=")
{
if (labText != "")
{
ui->btnNext->click();
return true;
}
else
{
return false;
}
}
else if (keyEvent->text() == "-" || keyEvent->text() == "_")
{
if (labText != "")
{
ui->btnPre->click();
return true;
}
else
{
return false;
}
}
else
{
if (currentEditType == "QWidget")
{
return false;
}
QString key;
if (inputType == "chinese")
{
key = keyEvent->text();
}
else if (inputType == "english")
{
if (upper)
{
key = keyEvent->text().toUpper();
}
else
{
key = keyEvent->text().toLower();
}
}
if (!key.isEmpty())
{
QList btn = this->findChildren();
foreach (QPushButton *b, btn)
{
QString text = b->text();
if (!text.isEmpty() && text == key)
{
b->click();
return true;
}
}
}
}
return false;
}
return QWidget::eventFilter(obj, event);
}
void frmInputNew::initProperty()
{
ui->btnDot->setProperty("btnOther", true);
ui->btnSpace->setProperty("btnOther", true);
ui->btnEnter->setPro