事件分发器——event()函数

事件过滤

事件进入窗口之前被拦截 eventFilter

#include "mywidget.h"
#include "ui_mywidget.h"
#include <QDebug> MyWidget::MyWidget(QWidget *parent) :
QWidget(parent),
ui(new Ui::MyWidget)
{
ui->setupUi(this);
// 给MyLabel 安装事件过滤器
// 参数 谁来过滤label 的事件
ui->mylabel->installEventFilter(this);
ui->label_2->installEventFilter(this);
} MyWidget::~MyWidget()
{
delete ui;
} void MyWidget::mousePressEvent(QMouseEvent *)
{
qDebug() << "+++++++++++++";
} bool MyWidget::eventFilter(QObject *obj, QEvent *e)
{
// 判断对象
if (obj == ui->mylabel) {
// 过滤事件
if (e->type() == QEvent::MouseMove)
{
ui->mylabel->setText("++++++++++");
return true;
}
}
if (obj == ui->label_2) {
if (e->type() == QEvent::MouseMove)
{
ui->label_2->setText("**********");
return true;
}
}
// 执行默认处理
return QWidget::eventFilter(obj,e);
}

mywidget.cpp

#include "mylabel.h"
#include <QMouseEvent>
#include <QTimerEvent>
#include <QTimer>
#include <QEvent> // QWidget 默认是不追踪鼠标事件的
MyLabel::MyLabel(QWidget *parent) : QLabel(parent)
{
// 设置窗口追踪鼠标键
this->setMouseTracking(true); // 启动定时器
// 参数 1: 触发定时器的时间, 单位: ms
// 参数2: 使用默认值
// 返回值: 定时器ID
id = startTimer();
id1 = startTimer(); // 第二种定时器用法
// QTimer * timer = new QTimer(this);
// timer->start(100);
// connect(timer, &QTimer::timeout, this, [=]()
// {
// static int number = 0;
// this->setText(QString::number(number++));
// }); } // 进入还是离开边界的一瞬间来完成的
// 鼠标进入
void MyLabel::enterEvent(QEvent *)
{
setText("你不要在我身上乱摸!!!!");
} // 鼠标离开
void MyLabel::leaveEvent(QEvent *)
{
setText("终于离开了...");
} void MyLabel::mousePressEvent(QMouseEvent *ev)
{
// 字符串拼接 QString().arg()
// %1, %2, %3 -- 占位符
QString btn;
if(ev->button() == Qt::LeftButton)
{
btn = "LeftButton";
}
else if(ev->button() == Qt::RightButton)
{
btn = "RightButton";
}
else if(ev->button() == Qt::MidButton)
{
btn = "MidButton";
}
QString str = QString("MousePree[%3]:(%1, %2)").arg(ev->x()).arg(ev->y()).arg(btn); setText(str);
} void MyLabel::mouseReleaseEvent(QMouseEvent *ev)
{
QString btn;
if(ev->button() == Qt::LeftButton)
{
btn = "LeftButton";
}
else if(ev->button() == Qt::RightButton)
{
btn = "RightButton";
}
else if(ev->button() == Qt::MidButton)
{
btn = "MidButton";
}
QString str = QString("MouseRelease[%3]:(%1, %2)").arg(ev->x()).arg(ev->y()).arg(btn); setText(str);
} void MyLabel::mouseMoveEvent(QMouseEvent *ev)
{
QString btn;
if(ev->buttons() & (Qt::LeftButton | Qt::RightButton))
{
btn = "LeftButton";
}
else if(ev->buttons() & Qt::RightButton)
{
btn = "RightButton";
}
else if(ev->buttons() & Qt::MidButton)
{
btn = "MidButton";
}
QString str = QString("MouseMove[%3]:(%1, %2)").arg(ev->x()).arg(ev->y()).arg(btn); setText(str);
} // 每触发一次定时器, 进入该函数中
void MyLabel::timerEvent(QTimerEvent *e)
{
QString str;
if(e->timerId() == id)
{
static int num = -;
str = QString("%1: %2").arg("Time out: ").arg(num++);
if(num >= )
{
// 关闭定时器
killTimer(id);
} }
else if(e->timerId() == id1)
{
static int num1 = ;
str = QString("%1: %2").arg("Time out: ").arg(num1++);
if(num1 >= +)
{
// 关闭定时器
killTimer(id1);
}
} setText(str);
} bool MyLabel::event(QEvent *e)
{
// 返回值
// true -- 代表事件被处理了,不再继续下发,停止了
// false -- 事件没有被处理,会继续向下分发
// 大概的处理步骤
// switch(e->type()){
// case QEvent::MouseMove:
// mouseMoveEvent(e);
// break;
// case QEvent::Timer:
// timerEvent(e);
// break;
// } // 过滤定时器事件
if (e->type() == QEvent::Timer) {
return true;// return false,将事件抛给父类
}
// else if (e->type() == QEvent::MouseMove) {
// return true;
// }
else if (e->type() == QEvent::MouseButtonPress) {
return false;
}
// 让父类执行默认的处理
return QLabel::event(e);
}

mylabel.cpp

#ifndef MYWIDGET_H
#define MYWIDGET_H #include <QWidget> namespace Ui {
class MyWidget;
} class MyWidget : public QWidget
{
Q_OBJECT public:
explicit MyWidget(QWidget *parent = nullptr);
~MyWidget();
void mousePressEvent(QMouseEvent *);
bool eventFilter(QObject *obj, QEvent *e);
private:
Ui::MyWidget *ui;
}; #endif // MYWIDGET_H

mywidget.h

#ifndef MYLABEL_H
#define MYLABEL_H #include <QWidget>
#include <QLabel> class MyLabel : public QLabel
{
Q_OBJECT
public:
explicit MyLabel(QWidget *parent = nullptr);
void enterEvent(QEvent *);
void leaveEvent(QEvent *);
void mousePressEvent(QMouseEvent *ev);
void mouseReleaseEvent(QMouseEvent *ev);
void mouseMoveEvent(QMouseEvent *ev);
void timerEvent(QTimerEvent *e);
bool event(QEvent *e);
private:
int id;
int id1;
signals: public slots:
}; #endif // MYLABEL_H

mylabel.h

(十三)事件分发器——event()函数,事件过滤的更多相关文章

  1. QT_8_Qt中的事件处理_定时器事件_定时器类_事件分发器_事件过滤器_绘图事件_高级绘图事件_绘图设备_QFile 文件读写_QFileInfo文件信息

    Qt中的事件处理 1.1. 捕获QLabel中是鼠标事件 1.2. enterevent 鼠标进入 1.3. leaveevent 鼠标离开 1.4. 鼠标按下MyLabel::mousePressE ...

  2. cocos2d JS 自定义事件分发器(接收与传递数据) eventManager

    简而言之,它不是由系统自动触发,而是人为的干涉 较多情况用于传递数据 var _listener1 = cc.EventListener.create({ event: cc.EventListene ...

  3. MySQL事件调度器Event Scheduler

    我们都知道windows的计划任务和linux的crontab都是用来实现一些周期性的任务和固定时间须要运行的任务. 在mysql5.1之前我们完毕数据库的周期性操作都必须借助这些操作系统实现. 在m ...

  4. Android事件分发机制三:事件分发工作流程

    前言 很高兴遇见你~ 本文是事件分发系列的第三篇. 在前两篇文章中,Android事件分发机制一:事件是如何到达activity的? 分析了事件分发的真正起点:viewRootImpl,Activit ...

  5. MySQL计划任务(事件调度器)(Event Scheduler)

    http://www.cnblogs.com/c840136/articles/2388512.html https://dev.mysql.com/doc/refman/5.7/en/events- ...

  6. MySQL事件调度器event的使用

    Q:假设,有一个需求,希望在某一个时刻系统调用一个begin end执行一下:十分钟以后执行一下begin end.亦或有一个需求,每个多长时间周期性执行begin end.那么这个时候该怎么办呢? ...

  7. MySQL计划任务(事件调度器)(Event Scheduler)[转]

    原文链接: http://www.cnblogs.com/c840136/articles/2388512.html MySQL5.1.x版本中引入了一项新特性EVENT,顾名思义就是事件.定时任务机 ...

  8. Android事件分发机制一:事件是如何到达activity的?

    事件分发,真的一定从Activity开始吗? 前言 很高兴遇见你~ 事件分发,android中一个老生常谈的话题了.基本的流程我们也都知道是从Activity开始分发,但有一个关键问题是:事件是如何到 ...

  9. Qt中事件处理的方法(三种处理方法,四种覆盖event函数,notify函数,event过滤,事件处理器。然后继续传递给父窗口。可观察QWidget::event的源码,它是虚拟保护函数,可改写)

    一.Qt中事件处理的方式   1.事件处理模式一 首先是事件源产生事件,最后是事件处理器对这些事件进行处理.然而也许大家会问, Qt中有这么多类的事件,我们怎么样比较简便的处理每个事件呢?设想,如果是 ...

随机推荐

  1. WPF:浅析Dispatcher

    本人文笔差.还是直接上代码吧.(本文假设你对WPF中的Dispatcher有一定的了解) 你觉得下面的代码可以正常执行吗? private void Button_Click(object sende ...

  2. 微信小程序开发基础

    前言: 微信小程序开入入门,如果你有html+css+javascript的基础,那么你就很快地上手掌握的.下面提供微信小程序官方地址:https://developers.weixin.qq.com ...

  3. SQLServer之删除用户定义的数据库角色

    删除用户定义的数据库角色注意事项 无法从数据库删除拥有安全对象的角色. 若要删除拥有安全对象的数据库角色,必须首先转移这些安全对象的所有权,或从数据库删除它们. 无法从数据库删除拥有成员的角色. 若要 ...

  4. vcenter 忘记 administrator@vsphere.local 密码怎么办

    现有一个windows版本的vcenter5.5管理员密码丢失,我们可以使用vmware的工具vdcadmintool,在命令行进入到vdcadmintool所在的目录,然后执行下vdcadminto ...

  5. bash: lspci: command not found解决方法

    在CentOS虚拟机使得lspci查看硬件信息.使用时,提示bash: lspci: command not found,大多使用/sbin/lspci即可,我发现我的系统中/sbin下也没有.使用y ...

  6. Microsoft Visual Studio 2012 添加实体数据模型

     Microsoft Visual Studio 2012 添加实体数据模型 1.创建一个web项目 2.添加ADO实体数据模型,如下图: 3.选择 从数据库生成,然后下一步 4.新建连接,如下图: ...

  7. Web Storage:浏览器端数据储存机制

    目录 概述 操作方法 存入/读取数据 清除数据 遍历操作 storage事件 参考链接 概述 这个API的作用是,使得网页可以在浏览器端储存数据.它分成两类:sessionStorage和localS ...

  8. C#:System.Array简单使用

    1.C# 中的数组 1.1 定义数组: 1.1.1 定义不初始化:数据类型[] 数组名称= new 数据类型[元素个数];    1.1.2 定义并初始化:数据类型[] 数组名称= new 数据类型[ ...

  9. #035 大数阶乘 PTA题目6-10 阶乘计算升级版 (20 分)

    实际题目 本题要求实现一个打印非负整数阶乘的函数. 函数接口定义: void Print_Factorial ( const int N ); 其中N是用户传入的参数,其值不超过1000.如果N是非负 ...

  10. python学习笔记5_异常

    python学习笔记5_异常 1.什么事异常 Python使用异常对象(exception object) 来表示异常情况.遇到错误会发生异常. 如果异常对象未被处理或被捕捉,程序就会用所谓的回溯(t ...