Qt中事件过滤器一般是用在继承QObject的类中,但是更多的是用在继承QWidget的界面类中。在界面类中,一般用的最多的是鼠标和键盘事件,包括鼠标摁下、移入、移除、拖拽等。一般在鼠标做出相应动作时,系统就会自动发送对应事件,然后被事件过滤器捕获,从而执行逻辑业务代码。
注意:事件过滤器作用域只在该界面内有效;出了该界面,在该界面重写的事件过滤器无效!!!
也即是如果我在QPushbutton内重写的事件过滤器,只能捕获该QPushbutton的各类事件,不能捕获该按钮以外的事件!
下面代码是Qt事件过滤器中所有事件枚举类型QEvent::type定义:
enum Type {
/*
If you get a strange compiler error on the line with None,
it's probably because you're also including X11 headers,
which #define the symbol None. Put the X11 includes after
the Qt includes to solve this problem.
*/
None = 0, // invalid event
Timer = 1, // timer event
MouseButtonPress = 2, // mouse button pressed
MouseButtonRelease = 3, // mouse button released
MouseButtonDblClick = 4, // mouse button double click
MouseMove = 5, // mouse move
KeyPress = 6, // key pressed
KeyRelease = 7, // key released
FocusIn = 8, // keyboard focus received
FocusOut = 9, // keyboard focus lost
FocusAboutToChange = 23, // keyboard focus is about to be lost
Enter = 10, // mouse enters widget
Leave = 11, // mouse leaves widget
Paint = 12, // paint widget
Move = 13, // move widget
Resize = 14, // resize widget
Create = 15, // after widget creation
Destroy = 16, // during widget destruction
Show = 17, // widget is shown
Hide = 18, // widget is hidden
Close = 19, // request to close widget
Quit = 20, // request to quit application
ParentChange = 21, // widget has been reparented
ParentAboutToChange = 131,