当前位置:网站首页>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");
}
}
我们来看一下运行效果
边栏推荐
猜你喜欢
用 SQL 做数据分析的十大常用功能,附面试原题解答!!
如何画一张架构图(内含知识图谱)
YYGH-BUG-06
Topic Modeling of Short Texts: A Pseudo-Document View
leetcode:149. 直线上最多的点数
5. Software testing ----- automated testing
QCheckBox、margin、border、pandding、QHoxLayout、QSplitter、QSpacerItem
孩子坐不住就是不专注?猿辅导揭秘专注力的三大误区
radio button、qss文件环境配置
Jenkins2.328+sonarqube7.9 实现代码自动化检测
随机推荐
【每日一题】622. 设计循环队列
[Static type and dynamic type compile check and run check in Objective-C]
网易数帆陈谔:云原生“牵手”低代码,加速企业数字化转型
leetcode:152. 乘积最大子数组
韦东山 数码相框 项目学习(五)libjpeg-turbo的移植
在VScode里调试ROS程序
45部署LVS-DR群集
【Flink】使用arthas在线诊断flink的那些事
【Arduino】重生之Arduino 学僧(3)----Arduino函数
[@property enhancement in Objective-C language]
工作两年成跳槽高峰期,程序员会在一家公司待多久?
【Arduino】重生之Arduino 学僧(2)----Arduino语言
FLIR E95 在8层楼看马路上行驶的CAR的热成像形态?
Linux定时任务脚本执行时mysqldump备份异常的问题
常用工具链和虚拟环境-WSL
禁用token及无感知更新token功能实现
SqlSession [[email protected]]
什么样的存储服务,才能成为企业数字化创新“加速器”?
Disable the token and update the token function without awareness
重定向printf到USB CDC、串口2