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()打印出来的值,下一篇文章将会用到。

相关推荐
古月-一个C++方向的小白6 小时前
C++11之lambda表达式与包装器
开发语言·c++
tanyongxi668 小时前
C++ AVL树实现详解:平衡二叉搜索树的原理与代码实现
开发语言·c++
斯是 陋室10 小时前
在CentOS7.9服务器上安装.NET 8.0 SDK
运维·服务器·开发语言·c++·c#·云计算·.net
tju新生代魔迷11 小时前
C++:list
开发语言·c++
HHRL-yx11 小时前
C++网络编程 5.TCP套接字(socket)通信进阶-基于多线程的TCP多客户端通信
网络·c++·tcp/ip
肥or胖11 小时前
【FFmpeg 快速入门】本地播放器 项目
开发语言·qt·ffmpeg·音视频
tomato0911 小时前
河南萌新联赛2025第(一)场:河南工业大学(补题)
c++·算法
每一天都要努力^14 小时前
C++拷贝构造
开发语言·c++
NoirSeeker15 小时前
在windows平台上基于OpenHarmony sdk编译三方库并暴露给ArkTS使用(详细)
c++·windows·arkts·鸿蒙·交叉编译
落羽的落羽15 小时前
【C++】(万字)一文看懂“类与对象”
c++