T06EventFilter.pro
1 HEADERS += \2 MyWidget.h3 4 SOURCES += \5 MyWidget.cpp6 7 QT += widgets gui
MyWidget.h
1 #ifndef MYWIDGET_H 2 #define MYWIDGET_H 3 4 #include5 #include 6 class MyWidget : public QWidget 7 { 8 Q_OBJECT 9 public:10 explicit MyWidget(QWidget *parent = nullptr);11 QPushButton* _button;12 bool eventFilter(QObject *, QEvent *);//原型13 signals:14 15 public slots:16 };17 18 #endif // MYWIDGET_H
MyWidget.cpp
1 #include "MyWidget.h" 2 #include3 #include 4 MyWidget::MyWidget(QWidget *parent) : QWidget(parent) 5 { 6 QPushButton* button; 7 button = new QPushButton("This button", this); 8 connect(button, SIGNAL(clicked()), this, SLOT(close())); 9 10 _button = button;11 /*button给自己安装了一个消息过滤器,经过button的消息,都先要调用它的过滤器eventFilter函数*/12 button->installEventFilter(this);13 }14 15 bool MyWidget::eventFilter(QObject *o, QEvent *e)//o谁的,e什么消息(对象,事件)16 {17 18 //(对象,事件)19 if (o == (QObject*)_button &&20 (e->type() == QEvent::MouseButtonPress ||21 e->type() == QEvent::MouseButtonRelease ||22 e->type() == QEvent::MouseButtonRelease ))//截断,单击,双击,不发生反应23 {24 return true;25 }26 27 return QWidget::eventFilter(o, e);28 }29 #include 30 int main(int argc, char* argv[])31 {32 QApplication app(argc, argv);33 34 MyWidget w;35 w.show();36 37 return app.exec();38 }
消息被过滤,单击,双击都没有反应。
如果将17~26行注释,单击按钮后,窗口立即消失。
notify:
MyApplication.h
1 #ifndef MYAPPLICATION_H 2 #define MYAPPLICATION_H 3 4 #include5 6 class MyApplication : public QApplication 7 { 8 Q_OBJECT 9 public:10 11 MyApplication(int argc, char* argv[]):QApplication(argc, argv)12 {}13 bool notify(QObject *, QEvent *);14 signals:15 16 public slots:17 };18 19 #endif // MYAPPLICATION_H
MyWidget.h
1 #ifndef MYWIDGET_H 2 #define MYWIDGET_H 3 4 #include5 #include 6 class MyWidget : public QWidget 7 { 8 Q_OBJECT 9 public:10 explicit MyWidget(QWidget *parent = nullptr);11 QPushButton* _button;12 bool eventFilter(QObject *, QEvent *);//原型13 bool event(QEvent *);//重载event函数14 signals:15 16 public slots:17 };18 19 #endif // MYWIDGET_H
MyApplication.cpp
1 #include "MyApplication.h" 2 #include3 #include 4 5 bool MyApplication::notify(QObject *o, QEvent *e) 6 { 7 if (this->topLevelWidgets().count()>0)//判断子窗口是否存在 8 { 9 QWidget* mainWnd = this->topLevelWidgets().at(0);//主窗口不在了,还会notify,会报错,需先判断10 if (o == (QObject*)mainWnd && e->type() == QEvent::MouseButtonPress)11 {12 qDebug() << "mainwnd is clicked";13 }14 }15 return QApplication::notify(o, e);16 }
MyWidget.cpp
1 #include "MyWidget.h" 2 #include3 #include 4 #include "MyApplication.h" 5 #include 6 MyWidget::MyWidget(QWidget *parent) : QWidget(parent) 7 { 8 QPushButton* button; 9 button = new QPushButton("This button", this);10 connect(button, SIGNAL(clicked()), this, SLOT(close()));11 12 _button = button;13 /*button给自己安装了一个消息过滤器,经过button的消息,都先要调用它的过滤器eventFilter函数*/14 button->installEventFilter(this);15 }16 17 bool MyWidget::eventFilter(QObject *o, QEvent *e)//o谁的,e什么消息(对象,事件)18 {19 20 //(对象,事件)21 if (o == (QObject*)_button &&22 (e->type() == QEvent::MouseButtonPress ||23 e->type() == QEvent::MouseButtonRelease ||24 e->type() == QEvent::MouseButtonRelease ))//截断,单击,双击,不发生反应25 {26 return true;27 }28 29 return QWidget::eventFilter(o, e);30 }31 32 bool MyWidget::event(QEvent *e)33 {34 if (e->type() == QEvent::User)35 {36 qDebug() << "User event is comming";37 }38 return QWidget::event(e);39 }40 41 int main(int argc, char* argv[])42 {43 MyApplication app(argc, argv);44 45 MyWidget w;46 w.show();47 //发送一个Event给MyWidget48 qDebug() << "begin send";49 //发给自定义消息给窗口50 app.postEvent(&w, new QEvent(QEvent::User));//不是立刻处理,加入消息队列等待处理,常用51 app.sendEvent(&w, new QEvent(QEvent::User));//立即处理52 qDebug() << "end send";53 54 return app.exec();55 }
运行后
第一个User event is comming来自于sendEvent函数,第二个来自于postEvent。
点击主窗口时输出mainwnd is clicked
欢迎交流。