Qt事件处理机制1-事件过滤器

事件在到达某个具体的对象前,可以通过事件过滤器进行筛选处理,函数定义如下:

cpp 复制代码
virtual bool eventFilter(QObject *watched, QEvent *event);

Qt帮助文档:

Filters events if this object has been installed as an event filter for the watched object. In your

reimplementation of this function, if you want to filter the event out, i.e. stop it being handled further, return true; otherwise return false.

大致意思 :如果在eventFilter函数中返回true,表示拦截事件,不再执行watched对象的event函数;而返回false,则表示除了在eventFilter中进行相关操作后,仍旧希望执行watched对象的event函数;也可以在eventFilter函数中调用父类的eventFilter函数,交给父类来处理,父类仍旧遵循以上规则。

下面关于文档里说的返回值,进行演示说明
MyButton.h

cpp 复制代码
#ifndef MYBUTTON_H
#define MYBUTTON_H

#include <QPushButton>

class MyButton : public QPushButton
{
    Q_OBJECT
public:
    MyButton(QWidget *parent = nullptr);
    ~MyButton();

protected:
    bool event(QEvent *e) override;
};

#endif // MYBUTTON_H

MyButton.cpp

cpp 复制代码
#include "MyButton.h"
#include <QDebug>
#include <QEvent>

MyButton::MyButton(QWidget *parent) : QPushButton(parent){}

MyButton::~MyButton(){}

bool MyButton::event(QEvent *e)
{
    if (e->type() == QEvent::MouseButtonPress)
    {
        qDebug() << __FUNCTION__ << event->isAccepted();
    }

    return QPushButton::event(e);
}

MainWindow.h

cpp 复制代码
#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = nullptr);
    ~MainWindow();

protected:
    bool eventFilter(QObject *watched, QEvent *event) override;

private:
    Ui::MainWindow *ui;
};

#endif // MAINWINDOW_H

MainWindow.cpp

cpp 复制代码
#include "MainWindow.h"
#include "ui_MainWindow.h"
#include <QDebug>

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    ui->pushButton->installEventFilter(this);
}

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

bool MainWindow::eventFilter(QObject *watched, QEvent *event)
{
    if (watched == ui->pushButton)
    {
        if (event->type() == QEvent::MouseButtonPress)
        {
            qDebug() << __FUNCTION__ << event->isAccepted();
            return false;// 注意这里的返回值,影响运行结果,文章最后会提到
        }
    }

    return QMainWindow::eventFilter(watched, event);
}

运行结果: eventFilter函数中return false时

cpp 复制代码
MainWindow::eventFilter true
MyButton::event true

运行结果: eventFilter函数中return true时

cpp 复制代码
MainWindow::eventFilter true

温馨提示:请注意程序中的event->isAccepted()打印出来的值,下一篇文章将会用到。

相关推荐
自信150413057592 小时前
重生之从0开始学习c++之模板初级
c++·学习
历程里程碑2 小时前
2. Git版本回退全攻略:轻松掌握代码时光机
大数据·c++·git·elasticsearch·搜索引擎·github·全文检索
极客智造2 小时前
深度解析 C++ 类继承与多态:面向对象编程的核心
c++
零号全栈寒江独钓5 小时前
基于c/c++实现linux/windows跨平台获取ntp网络时间戳
linux·c语言·c++·windows
CSCN新手听安5 小时前
【linux】高级IO,以ET模式运行的epoll版本的TCP服务器实现reactor反应堆
linux·运维·服务器·c++·高级io·epoll·reactor反应堆
雾岛听蓝6 小时前
Qt操作指南:窗口组成与菜单栏
开发语言·经验分享·笔记·qt
松☆7 小时前
C++ 算法竞赛题解:P13569 [CCPC 2024 重庆站] osu!mania —— 浮点数精度陷阱与 `eps` 的深度解析
开发语言·c++·算法
(Charon)7 小时前
【C++/Qt】C++/Qt 实现 TCP Server:支持启动监听、消息收发、日志保存
c++·qt·tcp/ip
并不喜欢吃鱼8 小时前
从零开始C++----七.继承及相关模型和底层(上篇)
开发语言·c++
tankeven9 小时前
HJ182 画展布置
c++·算法