当前位置:网站首页>QT之鼠标和键盘事件重写
QT之鼠标和键盘事件重写
2022-08-03 02:29:00 【天天进步一点点】
有时候我们需要重写键盘和鼠标的事件的处理函数,那么这个时候我们就需要进行事件编程
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QMouseEvent>
#include <QWheelEvent>
#include <QKeyEvent>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = nullptr);
~MainWindow();
protected:
//我们来重写QT的5个标准的鼠标事件函数,从而实现我们自己想要的动作
void mouseDoubleClickEvent(QMouseEvent *event);//鼠标双击事件
void mouseMoveEvent(QMouseEvent *event);//数据表移动事件
void mousePressEvent(QMouseEvent *event);//鼠标按下事件
void mouseReleaseEvent(QMouseEvent *event);//鼠标松开事件
void wheelEvent(QWheelEvent *event); //鼠标滚轮事件
void keyPressEvent(QKeyEvent *event);//键盘事件
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
setMouseTracking(true);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::mouseDoubleClickEvent(QMouseEvent *event)
{
if(event->button() == Qt::LeftButton){
ui->label->setText("left button dbclicked");
}
else {
ui->label->setText("right button dbclicked");
}
}
void MainWindow::mouseMoveEvent(QMouseEvent *event)
{
QPoint pos = event->globalPos();
ui->label_2->setText(QString("(%1,%2)").arg(pos.rx()).arg(pos.ry()));
}
void MainWindow::mousePressEvent(QMouseEvent *event)
{
if(event->button() == Qt::LeftButton){
ui->label->setText("left button pressed");
}
else {
ui->label->setText("right button pressed");
}
}
void MainWindow::mouseReleaseEvent(QMouseEvent *event)
{
if(event->button() == Qt::LeftButton){
ui->label->setText("left button released");
}
else {
ui->label->setText("right button released");
}
}
void MainWindow::wheelEvent(QWheelEvent *event)
{
if(event->delta() > 0){
ui->label_2->setText("wheel up");
}
else {
ui->label_2->setText("wheel down");
}
}
void MainWindow::keyPressEvent(QKeyEvent *event)
{
if(event->key() == Qt::Key_0)//键盘0按下
{
ui->label_2->setText("key 0 pressed");
}
if(event->key() == Qt::Key_1)
{
ui->label_2->setText("key 1 pressed");
}
}
我们来看一下运行效果

边栏推荐
- The LVS load balancing cluster and the deployment of the LVS - NAT experiment
- 【TA-霜狼_may-《百人计划》】美术2.5 模型常见问题及规范
- Incorrect datetime value: '2022-01-01' for function str_to_date
- C语言实验十三 指针(三)
- 【TA-霜狼_may-《百人计划》】先行部分 手搓视差体积云
- JS高级 之 Proxy-Reflect 使用详解
- 【Objective-C语言中的@property增强】
- 【云原生】阿里云ARMS业务实时监控
- 简单的布局的初级智能文本提示器
- qt opengl 使用不同的颜色绘制线框三角形
猜你喜欢
随机推荐
C语言入门--指针
numpy PIL tensor之间的相互转换
45部署LVS-DR群集
QWidget、QPushButton、
【Flink】使用arthas在线诊断flink的那些事
为什么要使用 playwright 做浏览器自动化测试?
C语言——-动态内存开辟与管理(malloc,free,calloc,realloc)+柔性数组
Rust Web(三)—— 通过sqlx连接数据库(MySQL)
DJI内推码(2022年8月2日更新)
【Arduino】重生之Arduino 学僧(3)----Arduino函数
【GO记录】从零开始GO语言——用GO语言做一个示波器(二)基于arduino的简易示波器
流程图(1)
Mysql-如何进行慢SQL查询
【GraphQL】使用Hot Chocolate和.NET 6构建GraphQL应用
rancher集成ldap,实现统一账号登录
力扣第二周错题集
05-分布式计算框架
MySQL-Explain详解
Disable the token and update the token function without awareness
Incorrect datetime value: '2022-01-01' for function str_to_date









