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

相关推荐
kaixin_learn_qt_ing8 分钟前
【银河麒麟下】 安装达梦数据库 + 使用Qt访问达梦数据库
qt
mengzhi啊12 分钟前
缓存类CacheDataManager把临时数据存入json。做成插件·
qt·缓存·json
ChampaignWolf19 分钟前
在 SAP S/4HANA 内部基于 ICF Handler 从零实现 ABAP MCP Server:架构、代码与踩坑实录
c++·架构·sap·abap·mcp
尘客-追梦1 小时前
Day 01:插件化之前,先看清 ABI 这堵墙
开发语言·c++·qt
有点。2 小时前
*C++哈夫曼树与哈夫曼编码
java·c++·servlet
啦啦啦啦啦zzzz2 小时前
利用RAII机制进行日志的采集
linux·服务器·c++·网络编程·c++20
nLif3 小时前
基于GTK的简易MFC对话框兼容层 -- MfcToGTK
linux·c++·mfc·gtk
Quz4 小时前
QML SwipeView:实现一个简单的图片浏览器
qt
Quz4 小时前
QML SwipeView:标签导航 与 Loader 懒加载
qt
小小、码农5 小时前
C++11右值引用与移动语义 —— 从拷贝资源到转移资源
开发语言·网络·c++·网络协议