h文件
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
#include <QKeyEvent>
namespace Ui {
class Widget;
}
class Widget : public QWidget
{
Q_OBJECT
public:
explicit Widget(QWidget *parent = nullptr);
~Widget();
private slots:
void on_pushButton_clicked(); //按钮按下执行一次
void on_pushButton_2_clicked(); //按钮按下执行多次
private:
Ui::Widget *ui;
void keyPressEvent(QKeyEvent *event); //windows Qt 键盘上上下左右键操作
void keyReleaseEvent(QKeyEvent *event);
};
#endif // WIDGET_H
cpp文件
#include "widget.h"
#include "ui_widget.h"
#include <QDebug>
Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{
ui->setupUi(this);
setFocusPolicy(Qt::StrongFocus);
ui->label->setText("50");
ui->label->setAlignment(Qt::AlignCenter);
ui->pushButton_2->setAutoRepeat(true);
ui->pushButton_2->setAutoRepeatDelay(500);
ui->pushButton_2->setAutoRepeatInterval(50);
}
Widget::~Widget()
{
delete ui;
}
void Widget::on_pushButton_clicked()
{
int num = ui->label->text().toInt();
num--;
ui->label->setText(QString::number(num));
}
void Widget::on_pushButton_2_clicked()
{
int num = ui->label->text().toInt();
num++;
ui->label->setText(QString::number(num));
}
void Widget::keyPressEvent(QKeyEvent *event)
{
setFocusPolicy(Qt::StrongFocus);
installEventFilter(this);
if(event->key() == Qt::Key_Up)
on_pushButton_2_clicked();
else if(event->key() == Qt::Key_Down)
on_pushButton_clicked();
event->accept();
}
void Widget::keyReleaseEvent(QKeyEvent *event)
{
}
Qt 按钮按下执行一次和多次的区别,windows获取到键盘上下左右键
最新推荐文章于 2024-07-02 16:54:50 发布