Qt中的事件(2)- 事件过滤器

在上一篇文章中,我们介绍了Qt的事件,事件的传递顺序,在对象处理事件前会经过对象过滤器来来判断事件是否向该对象中传递。本章我们来介绍一下Qt对象的事件过滤器。
上篇文章链接:
Qt中的事件(1)

事件过滤器, 可以处理或者分发特定的事件。
① 如果Qt没有提供和针对某个事件的特定的虚函数,我们可以使用事件过滤器来处理该事件。
② 可以在事件传递到某个对象前,处理该事件,并做出判断该事件是否被传递到该对象中。

事件过滤器必须是继承自QObject的类或者继承了QObject的派生类的类,通过重写QObject类的eventFilter方法接收事件处理。eventFilter函数的原型如下:
virtual bool eventFilter(QObject *watched, QEvent *event);
参数:watched是被监控的对象,event为事件
返回值:表示该事件是否被过滤。true, 表示该事件不传递到watched中,而false表示传递到watched对象中。

被监控的对象需要安装它的对象监视器,使用如下函数安装
void installEventFilter(QObject *);

下面是一个事件过滤器的例子,本章中在上一章的代码,添加的代码会在下面特别提出:
.h文件中的内容:

#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>
#include <QPushButton>

class PushButton : public QPushButton
{
    Q_OBJECT

public:
    PushButton(QWidget *parent = nullptr);
    ~PushButton();

protected:
    virtual void mousePressEvent(QMouseEvent *event);
};

// -----------------------------------------------------------------------------------
class Widget : public QWidget
{
    Q_OBJECT

public:
    Widget(QWidget *parent = 0);
    ~Widget();

protected:
    virtual void mousePressEvent(QMouseEvent *event);


private:
    QPushButton *m_Button = nullptr;
};

// -----------------------------------------------------------------------------------
class EventWatcher : public QObject
{
    Q_OBJECT

public:
    EventWatcher(QObject *parent = nullptr);
    ~EventWatcher();

protected:
    virtual bool eventFilter(QObject *obj, QEvent *event);
};

#endif // WIDGET_H

.cp中的内容:

#include "widget.h"
#include <QVBoxLayout>
#include <QDebug>
#include <QMouseEvent>

PushButton::PushButton(QWidget *parent)
    :QPushButton(parent)
{
    this->setFixedSize(200, 200);
}

PushButton::~PushButton()
{

}

void PushButton::mousePressEvent(QMouseEvent *event)
{
    QPushButton::mousePressEvent(event);
    qDebug() << "MousePressed " << event->pos().x() << ", " << event->pos().y();
    event->ignore();
}

// -----------------------------------------------------------------------------------
Widget::Widget(QWidget *parent)
    : QWidget(parent)
{
    m_Button = new PushButton(this);
    m_Button->setText("OK");

    // 安装过滤器
    EventWatcher *watcher = new EventWatcher(this);
    m_Button->installEventFilter(watcher);

    QVBoxLayout *layout = new QVBoxLayout(this);
    layout->addWidget(m_Button);

    QObject::connect(m_Button, &QPushButton::clicked, [=](void)->void{
       qDebug() << "PushButton Pressed!" ;
    });

    this->setGeometry(100, 100, 800, 600);
}

Widget::~Widget()
{

}

void Widget::mousePressEvent(QMouseEvent *event)
{
    int xPt = event->pos().x();
    int yPt = event->pos().y();
    qDebug() << "QWidget " << xPt << ", " << yPt;
    return QWidget::mousePressEvent(event);
}

// -----------------------------------------------------------------------------------
EventWatcher::EventWatcher(QObject *parent)
    :QObject(parent)
{

}

EventWatcher::~EventWatcher()
{

}

bool EventWatcher::eventFilter(QObject *obj, QEvent *event)
{
    if (event->type() == QEvent::MouseButtonPress || event->type() == QEvent::MouseButtonDblClick)
    {
        return true;
    }

    return QObject::eventFilter(obj, event);
}

从头文件的第37行开始,添加了一个监控者的类,源文件中的第32行中,按钮对象添加了该事件过滤器;
按钮的点击和双击事件被监控不向按钮这个对象传递,因此程序的运行效果为点击按钮无任何反应。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Qt框架事件过滤器是一种高级机制,它允许你在应用程序拦截并处理特定类型的事件,包括滚动事件。当用户操作滚轮时,Qt会生成`QWheelEvent`对象。如果你想要捕获这个滚轮点击事件,并对用户的滚动动作做出响应,你可以这样做: 1. 首先,在需要添加事件过滤器的窗口或视图上设置一个公共槽函数,这个槽函数将接收滚动事件。 ```cpp void myWindow::wheelEvent(QWheelEvent *event) { // 在这里编写处理滚动事件的逻辑 } ``` 2. 接着,你需要创建一个`QObject`的派生类,并实现`QObject::installEventFilter()`方法,该方法会安装一个事件过滤器。在这个过滤器里,检查事件是否是你感兴趣的滚轮事件,如果是,就调用上面的槽函数。 ```cpp class MyEventFilter : public QObject { public: explicit MyEventFilter(QWidget *parent = nullptr) : QObject(parent) {} bool eventFilter(QObject *watched, QEvent *event) override { if (event->type() == QEvent::Wheel) { QWheelEvent *wheelEvent = static_cast<QWheelEvent*>(event); wheelEvent->accept(); // 允许事件继续传递到下一个事件处理器 emit wheelMoved(wheelEvent); // 调用槽函数并将事件传递给它 return true; // 返回true表示我们已经处理了事件 } return QObject::eventFilter(watched, event); } }; ``` 3. 然后,在窗口初始化时,将你的事件过滤器安装到窗口或其他需要监听滚轮事件的组件上。 ```cpp MyEventFilter *filter = new MyEventFilter(this); connect(filter, &MyEventFilter::wheelMoved, this, &myWindow::wheelEvent); this->installEventFilter(filter); ``` 这样,每当发生滚轮点击事件时,你的自定义槽函数`wheelEvent`就会被调用,你可以根据接收到的`QWheelEvent`对象来定制滚动行为。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值