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

相关推荐
2401_881244401 小时前
牛客周赛99
c++
山登绝顶我为峰 3(^v^)34 小时前
如何录制带备注的演示文稿(LaTex Beamer + Pympress)
c++·线性代数·算法·计算机·密码学·音视频·latex
十五年专注C++开发6 小时前
CMake基础:条件判断详解
c++·跨平台·cmake·自动化编译
QuantumStack9 小时前
【C++ 真题】P1104 生日
开发语言·c++·算法
天若有情6739 小时前
01_软件卓越之道:功能性与需求满足
c++·软件工程·软件
whoarethenext9 小时前
使用 C++/OpenCV 和 MFCC 构建双重认证智能门禁系统
开发语言·c++·opencv·mfcc
Jay_51510 小时前
C++多态与虚函数详解:从入门到精通
开发语言·c++
追风赶月、11 小时前
【QT】事件(鼠标、按键、定时器、窗口)
qt
xiaolang_8616_wjl11 小时前
c++文字游戏_闯关打怪
开发语言·数据结构·c++·算法·c++20
FrostedLotus·霜莲11 小时前
C++主流编辑器特点比较
开发语言·c++·编辑器