其他分享
首页 > 其他分享> > 【QT学习】QKeyEvent键盘事件制作简单软件盘

【QT学习】QKeyEvent键盘事件制作简单软件盘

作者:互联网

文章目录

QKeyEvent键盘事件

改变焦点的方式以及策略(focusPolicy)

简单软键盘(部分按键)制作代码:

自定义派生PushButton类的头文件btkey.h

#ifndef BTKEY_H
#define BTKEY_H

#include <QPushButton>
#include <QKeyEvent>
#include <QApplication>
#include <QObject>
#include "mykeyboard.h"

#include <QDebug>

class Btkey : public QPushButton
{
    Q_OBJECT

public:
    // 用两个bool静态成员来表示键盘的caps和shift键是否按下
    static bool capstatus;
    static bool shiftstatus;

    Btkey(QWidget *parent = nullptr);
    ~Btkey();

private slots:
    void handler_slot(bool k);
};

#endif // BTKEY_H

自定义派生PushButton类的源文件btkey.cpp

#include "btkey.h"

bool Btkey::capstatus = false;
bool Btkey::shiftstatus = false;

Btkey::Btkey(QWidget *parent) :
    QPushButton(parent)
{
    // 将按钮的颜色置为半透明的蓝色
    this->setStyleSheet("background-color: rgba(153, 215, 255, 150);");
    // 设置焦点获得策略
    this->setFocusPolicy(Qt::NoFocus);

    // 链接槽函数
    connect(this, SIGNAL(clicked(bool)), this, SLOT(handler_slot(bool)));
}

Btkey::~Btkey()
{
    this->destroy();
}

void Btkey::handler_slot(bool k)
{
    QApplication::keyboardModifiers();
    auto *objfous = QApplication::focusWidget();
    bool ok;
    int key = this->text().toInt(&ok, 16);
    QString text(this->text());

    qDebug() << this->text();
    if(this->text() == "CapsLK"){
        qDebug() << "按下CapsLk键";
        capstatus = k;
        qobject_cast<MyKeyBoard *>(this->parent())->letterToggle();
        text = "";
    }else if(this->text() == "Shift"){
        qDebug() << "按下Shift键";
        shiftstatus = k;
        qobject_cast<MyKeyBoard *>(this->parent())->letterToggle();
        key = Qt::Key_Shift;
        text = "";
    }else if(this->text() == "Enter"){
        qDebug() << "按下Enter键";
        key = Qt::Key_Enter;
    }else if(this->text() == "Backspace"){
        qDebug() << "按下BackSpace键";
        key = Qt::Key_Backspace;
    }else if(this->text() == "Tab"){
        qDebug() << "按下Tab键";
        key = Qt::Key_Tab;
    }else{
        qDebug() << "按下数字、字母或符号";
    }
    // 创建相应的按键事件对象
	QKeyEvent *event = new QKeyEvent(QEvent::KeyPress, key
			 , Qt::NoModifier, text
			 , false, 1);

	// fousWidget() 返回光标所在的对象
    // 派发事件
	QCoreApplication::postEvent(objfous, event);

    // 如果shift被按下,且又按下其他键,将让shift键弹起
	if(shiftstatus && key != Qt::Key_Shift){
	    shiftstatus = false;
	    qobject_cast<MyKeyBoard *>(this->parent())->shift_recover();
	    qobject_cast<MyKeyBoard *>(this->parent())->letterToggle();
	}
}

软键盘类的头文件mykeyboard.h

#ifndef MYKEYBOARD_H
#define MYKEYBOARD_H

#include <QWidget>
#include "btkey.h"
#include <QButtonGroup>
#include <QBrush>

QT_BEGIN_NAMESPACE
namespace Ui { class MyKeyBoard; }
QT_END_NAMESPACE

class MyKeyBoard : public QWidget
{
    Q_OBJECT

public:
    MyKeyBoard(QWidget *parent = nullptr);
    ~MyKeyBoard();

    void letterToggle(void);
    void shift_recover(void);

private slots:

private:
    Ui::MyKeyBoard *ui;
    QButtonGroup *Btletter;
    QList<QPushButton *> *Btlist;
};
#endif // MYKEYBOARD_H

软键盘类的源文件mykeyboard.cpp

#include "mykeyboard.h"
#include "ui_mykeyboard.h"

MyKeyBoard::MyKeyBoard(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::MyKeyBoard)
    , Btletter(new QButtonGroup(this))
    , Btlist(new QList<QPushButton *>())
{
    ui->setupUi(this);
    /* 按钮的布局以及部分属性定义(autorepeat属性,让按下持续输出)在uidesigner中设计*/

    // 将字母按键放到一个按键组方便大小写切换
    {
        Btletter->addButton(ui->key_a, 0);
        Btletter->addButton(ui->key_b, 1);
        Btletter->addButton(ui->key_c, 2);
        Btletter->addButton(ui->key_d, 3);
        Btletter->addButton(ui->key_e, 4);
        Btletter->addButton(ui->key_f, 5);
        Btletter->addButton(ui->key_g, 6);
        Btletter->addButton(ui->key_h, 7);
        Btletter->addButton(ui->key_i, 8);
        Btletter->addButton(ui->key_j, 9);
        Btletter->addButton(ui->key_k, 10);
        Btletter->addButton(ui->key_l, 11);
        Btletter->addButton(ui->key_n, 12);
        Btletter->addButton(ui->key_m, 13);
        Btletter->addButton(ui->key_o, 14);
        Btletter->addButton(ui->key_p, 15);
        Btletter->addButton(ui->key_q, 16);
        Btletter->addButton(ui->key_r, 17);
        Btletter->addButton(ui->key_s, 18);
        Btletter->addButton(ui->key_t, 19);
        Btletter->addButton(ui->key_u, 20);
        Btletter->addButton(ui->key_v, 21);
        Btletter->addButton(ui->key_w, 22);
        Btletter->addButton(ui->key_x, 23);
        Btletter->addButton(ui->key_y, 24);
        Btletter->addButton(ui->key_z, 25);
    }

    // 将shift和capslk键设置为可校验(按下不自动弹起)
    ui->key_capslk->setCheckable(true);
    ui->key_shift->setCheckable(true);

    // 设置背景为透明
    this->setAutoFillBackground(true);
    QPalette palette;
    palette.setBrush(QPalette::Background, QBrush(QColor(0,0,0,0)));
    this->setPalette(palette);
}

MyKeyBoard::~MyKeyBoard()
{
    delete ui;
}

// 实现CapsLk和Shift键的大小写翻转
void MyKeyBoard::letterToggle()
{
    for(int i = 0; i < 26; i++){
        QString c = Btletter->button(i)->text();
        // 等价于逻辑异或
        if(Btkey::capstatus ^ Btkey::shiftstatus)
            Btletter->button(i)->setText(c.toUpper());
        else
            Btletter->button(i)->setText(c.toLower());
    }
}

void MyKeyBoard::shift_recover()
{
    ui->key_shift->setChecked(false);
}

标签:key,addButton,QT,text,键盘,ui,Btletter,QKeyEvent,Qt
来源: https://blog.csdn.net/ZZ123ZZWW/article/details/122377242