T06EventFilter.pro
HEADERS += \
MyWidget.h SOURCES += \
MyWidget.cpp QT += widgets gui
MyWidget.h
#ifndef MYWIDGET_H
#define MYWIDGET_H #include <QWidget>
#include <QPushButton>
class MyWidget : public QWidget
{
Q_OBJECT
public:
explicit MyWidget(QWidget *parent = nullptr);
QPushButton* _button;
bool eventFilter(QObject *, QEvent *);//原型
signals: public slots:
}; #endif // MYWIDGET_H
MyWidget.cpp
#include "MyWidget.h"
#include <QPushButton>
#include <QEvent>
MyWidget::MyWidget(QWidget *parent) : QWidget(parent)
{
QPushButton* button;
button = new QPushButton("This button", this);
connect(button, SIGNAL(clicked()), this, SLOT(close())); _button = button;
/*button给自己安装了一个消息过滤器,经过button的消息,都先要调用它的过滤器eventFilter函数*/
button->installEventFilter(this);
} bool MyWidget::eventFilter(QObject *o, QEvent *e)//o谁的,e什么消息(对象,事件)
{ //(对象,事件)
if (o == (QObject*)_button &&
(e->type() == QEvent::MouseButtonPress ||
e->type() == QEvent::MouseButtonRelease ||
e->type() == QEvent::MouseButtonRelease ))//截断,单击,双击,不发生反应
{
return true;
} return QWidget::eventFilter(o, e);
}
#include <QApplication>
int main(int argc, char* argv[])
{
QApplication app(argc, argv); MyWidget w;
w.show(); return app.exec();
}
消息被过滤,单击,双击都没有反应。
如果将17~26行注释,单击按钮后,窗口立即消失。
notify:
MyApplication.h
#ifndef MYAPPLICATION_H
#define MYAPPLICATION_H #include <QApplication> class MyApplication : public QApplication
{
Q_OBJECT
public: MyApplication(int argc, char* argv[]):QApplication(argc, argv)
{}
bool notify(QObject *, QEvent *);
signals: public slots:
}; #endif // MYAPPLICATION_H
MyWidget.h
#ifndef MYWIDGET_H
#define MYWIDGET_H #include <QWidget>
#include <QPushButton>
class MyWidget : public QWidget
{
Q_OBJECT
public:
explicit MyWidget(QWidget *parent = nullptr);
QPushButton* _button;
bool eventFilter(QObject *, QEvent *);//原型
bool event(QEvent *);//重载event函数
signals: public slots:
}; #endif // MYWIDGET_H
MyApplication.cpp
#include "MyApplication.h"
#include <QEvent>
#include <QDebug> bool MyApplication::notify(QObject *o, QEvent *e)
{
if (this->topLevelWidgets().count()>)//判断子窗口是否存在
{
QWidget* mainWnd = this->topLevelWidgets().at();//主窗口不在了,还会notify,会报错,需先判断
if (o == (QObject*)mainWnd && e->type() == QEvent::MouseButtonPress)
{
qDebug() << "mainwnd is clicked";
}
}
return QApplication::notify(o, e);
}
MyWidget.cpp
#include "MyWidget.h"
#include <QPushButton>
#include <QEvent>
#include "MyApplication.h"
#include <QDebug>
MyWidget::MyWidget(QWidget *parent) : QWidget(parent)
{
QPushButton* button;
button = new QPushButton("This button", this);
connect(button, SIGNAL(clicked()), this, SLOT(close())); _button = button;
/*button给自己安装了一个消息过滤器,经过button的消息,都先要调用它的过滤器eventFilter函数*/
button->installEventFilter(this);
} bool MyWidget::eventFilter(QObject *o, QEvent *e)//o谁的,e什么消息(对象,事件)
{ //(对象,事件)
if (o == (QObject*)_button &&
(e->type() == QEvent::MouseButtonPress ||
e->type() == QEvent::MouseButtonRelease ||
e->type() == QEvent::MouseButtonRelease ))//截断,单击,双击,不发生反应
{
return true;
} return QWidget::eventFilter(o, e);
} bool MyWidget::event(QEvent *e)
{
if (e->type() == QEvent::User)
{
qDebug() << "User event is comming";
}
return QWidget::event(e);
} int main(int argc, char* argv[])
{
MyApplication app(argc, argv); MyWidget w;
w.show();
//发送一个Event给MyWidget
qDebug() << "begin send";
//发给自定义消息给窗口
app.postEvent(&w, new QEvent(QEvent::User));//不是立刻处理,加入消息队列等待处理,常用
app.sendEvent(&w, new QEvent(QEvent::User));//立即处理
qDebug() << "end send"; return app.exec();
}
运行后
第一个User event is comming来自于sendEvent函数,第二个来自于postEvent。
点击主窗口时输出mainwnd is clicked
欢迎交流。