-
Notifications
You must be signed in to change notification settings - Fork 364
/
main.cpp
105 lines (92 loc) · 2.33 KB
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#include <QApplication>
#include <QWidget>
#include <QPushButton>
#include <QMouseEvent>
#include <QDebug>
int counter = 0;
class MyButton : public QPushButton
{
Q_OBJECT
public:
using QPushButton::QPushButton;
bool event(QEvent *evt) override{
if(evt->type()==QEvent::MouseButtonPress){
//4
qDebug()<<++counter<<__FUNCTION__<<evt;
}
return QPushButton::event(evt);
}
protected:
void mousePressEvent(QMouseEvent *evt) override{
//5
qDebug()<<++counter<<__FUNCTION__<<evt;
QPushButton::mousePressEvent(evt);
}
};
class MyWidget : public QWidget
{
Q_OBJECT
public:
MyWidget(QWidget *parent = nullptr)
: QWidget(parent)
, button(new MyButton("press", this)){
button->installEventFilter(this);
}
bool eventFilter(QObject *watched, QEvent *evt) override{
if(watched == button && evt->type()==QEvent::MouseButtonPress){
//3
qDebug()<<++counter<<__FUNCTION__<<evt;
}
return false;
}
private:
QPushButton *button;
};
class MyApp : public QApplication
{
Q_OBJECT
public:
using QApplication::QApplication;
bool event(QEvent * evt) override{
//仅处理qApp相关的事件,本例的点击不会触发
if(evt->type()==QEvent::MouseButtonPress){
qDebug()<<++counter<<__FUNCTION__<<evt;
}
return QApplication::event(evt);
}
bool notify(QObject *obj, QEvent *evt) override{
if(evt->type()==QEvent::MouseButtonPress){
//1
qDebug()<<++counter<<__FUNCTION__<<obj<<evt;
}
return QApplication::notify(obj, evt);
}
};
class MyFilter : public QObject
{
Q_OBJECT
public:
using QObject::QObject;
bool eventFilter(QObject *obj, QEvent *evt) override{
if(evt->type()==QEvent::MouseButtonPress){
//2
qDebug()<<++counter<<__FUNCTION__<<obj<<evt;
}
return false;
}
};
int main(int argc, char *argv[])
{
MyApp app(argc, argv);
MyFilter filter;
app.installEventFilter(&filter);
MyWidget outer;
outer.setObjectName("outer");
outer.resize(300, 200);
MyWidget inner(&outer);
inner.setObjectName("inner");
inner.resize(300, 200);
outer.show();
return app.exec();
}
#include "main.moc"