头文件
#pragma once
#include "ui_KeyBoard.h"
#include <QWidget>
class QStackedWidget;
class QLabel;
class QLineEdit;
class QPushButton;
class QComboBox;
// 按钮的边长,键盘总长度=14*BTN_SIZE,键盘总宽度=3*BTN_SIZE
#define BTN_SIZE 40
class KeyBoard : public QWidget
{
Q_OBJECT
public:
explicit KeyBoard(QWidget *parent = 0);
~KeyBoard();
protected:
void mouseMoveEvent(QMouseEvent *e);
void mousePressEvent(QMouseEvent *e);
void mouseReleaseEvent(QMouseEvent *);
private slots:
void focusChanged(QWidget *, QWidget *nowWidget);
void slotBtnClicked(); // 按键处理
private:
int deskWidth; //桌面宽度
int deskHeight; //桌面高度
int frmWidth; //窗体宽度
int frmHeight; //窗体高度
QPoint mousePoint; //鼠标拖动自定义标题栏时的坐标
bool mousePressed; //鼠标是否按下
void InitWindow(); //初始化无边框窗体
void InitForm(); //初始化窗体数据
QLineEdit *currentLineEdit; //当前焦点的文本框
QString currentType; //当前输入法类型
void changeType(QString type); //改变输入法类型
void changeLetter(bool isUpper);//改变字母大小写
void changeStyle(int style); //切换样式处理
void setStyle(QString topColor, QString bottomColor, QString borderColor, QString textColor);
int currentStyle = 0;
QStackedWidget *keyWindow; // 键盘窗口,可以翻页显示
QWidget *letterWindow; // 字母键盘
QWidget *signWindow; // 字母键盘
QLabel *infoLabel; // 显示键盘信息
QPushButton *closeBtn;
QPushButton *delBtn;
QPushButton *typeBtn;
QPushButton *styleBtn;
QPushButton *btn0;
QPushButton *btn1;
QPushButton *btn9;
QPushButton *btnA;
QPushButton *btnZ;
QPushButton *btnSign0;
QPushButton *btnSign12;
};
cpp文件
#include "KeyBoard.h"
#include "Global.h"
#include <QDesktopWidget>
#include <QStackedWidget>
KeyBoard::KeyBoard(QWidget *parent) :
QWidget(parent)
{
this->InitWindow();
this->InitForm();
//setStyleSheet("background-image:url(:/res/img/bg_matel.png);");
QDesktopWidget* w = QApplication::desktop();
deskWidth = w->screenGeometry().width();
deskHeight = w->screenGeometry().height();
frmWidth = this->width();
frmHeight = this->height();
}
KeyBoard::~KeyBoard()
{
}
void KeyBoard::InitWindow()
{
this->setProperty("Form", true);
this->setWindowFlags(Qt::Tool | Qt::WindowStaysOnTopHint | Qt::FramelessWindowHint);
this->setFixedSize(14 * BTN_SIZE, 3 * BTN_SIZE);
this->setFocusPolicy(Qt::NoFocus);
setWindowOpacity(0.8);
setStyleSheet(QString("font-size:%1px;").arg(GlobalDefine::FONT_SIZE_TEXT));
keyWindow = new QStackedWidget(this);
keyWindow->setFixedSize(13 * BTN_SIZE, 2 * BTN_SIZE);
letterWindow = new QWidget;
signWindow = new QWidget;
// 填加功能按钮
closeBtn = new QPushButton(this);
closeBtn->setObjectName("closeBtn");
closeBtn->setProperty("function", true);
closeBtn->setText(tr("关闭"));
closeBtn->setFixedSize(BTN_SIZE, BTN_SIZE);
// 删除一个字符
delBtn = new QPushButton(this);
delBtn->setObjectName("delBtn");
delBtn->setProperty("function", true);
delBtn->setText(tr("退格"));
delBtn->setFixedSize(BTN_SIZE, BTN_SIZE);
// 改变输法类型:大写,小写,字符
typeBtn = new QPushButton(this);
typeBtn->setObjectName("typeBtn");
typeBtn->setProperty("function", true);
typeBtn->setText(tr("小"));
typeBtn->setFixedSize(BTN_SIZE, BTN_SIZE);
// 换肤
styleBtn = new QPushButton(this);
styleBtn->setObjectName("styleBtn");
styleBtn->setProperty("function", true);
styleBtn->setText(tr("风格"));
styleBtn->setFixedSize(BTN_SIZE, BTN_SIZE);
// 填加数字键盘
/*btn1 = new QPushButton(this);
btn1->setText(tr("1"));
btn1->setProperty("num", true);
btn1->setFixedSize(BTN_SIZE, BTN_SIZE);*/
QGridLayout* layout = new QGridLayout(this);
for (int i = 0; i < 10 ; ++i)
{
QPushButton *pbtn = new QPushButton(QString::number(i), this);
pbtn->setFixedSize(BTN_SIZE, BTN_SIZE);
layout->addWidget(pbtn, 0, i+1);
}
for (int i = 0,c = 'a'; i < 26; ++i,++c)
{
QPushButton *pbtn = new QPushButton(QString(c), this);
pbtn->setFixedSize(BTN_SIZE, BTN_SIZE);
layout->addWidget(pbtn, 1 + i/13, i%13);
}
layout->addWidget(delBtn, 0, 11, 1, 1);
layout->addWidget(closeBtn, 0, 12, 1, 1);
layout->addWidget(typeBtn, 1, 13, 1, 1);
layout->addWidget(styleBtn, 2, 13, 1, 1);
layout->addWidget(keyWindow, 1, 0, 2, 13);
layout->setSpacing(0);
layout->setContentsMargins(0, 0, 0, 0);
this->setLayout(layout);
}
void KeyBoard::InitForm()
{
currentStyle = 0;
currentLineEdit = 0;
mousePressed = false;
currentType = "min";
changeType("min");
currentStyle = 0;
changeStyle(currentStyle);
QList<QPushButton *> btn = this->findChildren<QPushButton *>();
foreach(QPushButton * b, btn) {
connect(b, SIGNAL(clicked()), this, SLOT(slotBtnClicked()));
}
// 绑定全局改变焦点信号槽
connect(qApp, SIGNAL(focusChanged(QWidget *, QWidget *)),
this, SLOT(focusChanged(QWidget *, QWidget *)));
}
void KeyBoard::focusChanged(QWidget *, QWidget *nowWidget)
{
if (nowWidget != 0 && !this->isAncestorOf(nowWidget))
{
if (nowWidget->inherits("QLineEdit"))
{
currentLineEdit = (QLineEdit *)nowWidget;
QPoint movePoint;
// 鼠标点击位置坐标
if (QCursor::pos().y() > deskHeight / 2)
{
// 靠上居中显示
movePoint = QPoint(deskWidth / 2 - frmWidth / 2, 0);
}
else
{
// 靠下居中显示
movePoint = QPoint(deskWidth / 2 - frmWidth / 2, deskHeight - frmHeight);
}
this->move(movePoint);
this->repaint();
this->setVisible(true);
}
else
{
currentLineEdit = 0;
//qDebug() << "BBB";
this->setVisible(false);
// 需要将输入法切换到最初的原始状态--小写
currentType = "min";
changeType(currentType);
currentStyle = 0;
changeStyle(currentStyle);
keyWindow->setCurrentIndex(0);
}
}
}
void KeyBoard::slotBtnClicked()
{
QPushButton *btn = (QPushButton *)sender();
QString objectName = btn->objectName();
if (objectName == "typeBtn")
{
if (currentType == "min")
{
currentType = "max";
QList<QPushButton *> btn = this->findChildren<QPushButton *>();
foreach(QPushButton * b, btn) {
QString str = b->text();
if (str.length() == 1 && str.at(0) >= 'a' && str.at(0) <= 'z')
{
char tc = str.toStdString().at(0);
b->setText(QString(tc + ('A' - 'a')));
}
}
}
else if (currentType == "max")
{
currentType = "min";
QList<QPushButton *> btn = this->findChildren<QPushButton *>();
foreach(QPushButton * b, btn) {
QString str = b->text();
if (str.length() == 1 && str.at(0) >= 'A' && str.at(0) <= 'Z')
{
char tc = str.toStdString().at(0);
b->setText(QString(tc - ('A' - 'a')));
}
}
}
//changeType(currentType);
}
else if (objectName == "delBtn")
{
int curpos = currentLineEdit->cursorPosition();
QString str = currentLineEdit->text();
QString strselect = currentLineEdit->selectedText();
if (!strselect.isEmpty())
{
int index = currentLineEdit->selectionStart() > currentLineEdit->selectionEnd() ?
currentLineEdit->selectionEnd() : currentLineEdit->selectionStart();
str = str.left(index) + str.right(str.length() - currentLineEdit->selectionLength() - index);
currentLineEdit->setText(str);
currentLineEdit->setCursorPosition(index);
}
else
{
if (str.length() > 0 && curpos > 0)
{
QString strl = str.left(curpos - 1);
QString strr = str.right(str.length() - curpos);
str = strl + strr;
}
currentLineEdit->setText(str);
currentLineEdit->setCursorPosition(curpos - 1);
}
//获取当前文bai本光标
//QCursor cursor = currentLineEdit->cursor();
判断当前是否选中了文本,如果选中了文本则取消选中的文本,再删除前一个字符
//if (cursor.hasSelection())
// cursor.clearSelection();
删除前一个字符
//cursor.deletePreviousChar();
设置当前的光标为更改后的光标
//currentLineEdit->setCursor(cursor);
}
else if (objectName == "closeBtn")
{
setVisible(false);
}
else if (objectName == "styleBtn")
{
if (currentStyle == 0)
{
currentStyle = 1;
setStyleSheet("background-color:rgba(0,0,0,0.5);color:rgb(255,255,255);border:1px solid rgba(255, 255, 255, 1)");
}
else
{
currentStyle = 0;
setStyleSheet("background-color:rgba(255,255,255,0.5);color:rgb(0,0,0);border: 1px solid rgba(0, 0, 0, 1)");
}
}
else
{
QString value = btn->text();
// 如果是&按钮,因为对应&被过滤,所以真实的text为去除前面一个&字符
if (value == "&&")
{
value = value.right(1);
}
// 当前不是中文模式,则单击按钮对应text为传递参数
if (currentType != "chinese")
{
if (currentLineEdit != 0)
{
currentLineEdit->insert(value);
}
}
}
}
void KeyBoard::mouseMoveEvent(QMouseEvent *e)
{
}
void KeyBoard::mousePressEvent(QMouseEvent *e)
{
}
void KeyBoard::mouseReleaseEvent(QMouseEvent *)
{
}
void KeyBoard::changeType(QString type)
{
}
void KeyBoard::changeLetter(bool isUpper)
{
}
void KeyBoard::changeStyle(int style)
{
}
void KeyBoard::setStyle(QString topColor, QString bottomColor, QString borderColor, QString textColor)
{
}