当前位置:网站首页>Event Summary - common summary
Event Summary - common summary
2022-07-27 05:02:00 【PHP code】
Qt This is the whole Qt One of the core mechanisms of the framework , It's a little bit more complicated . Say it's complicated , More because it involves many functions , And there are many ways to deal with it , Sometimes it's hard to choose . Now let's summarize briefly Qt Event mechanism in .
Qt There are many kinds of events in the world : Mouse events 、 Keyboard events 、 Events of size change 、 Location movement events and so on . How to deal with these events , There are actually two options :
- All events correspond to one event handler , In this event handler, a large branch statement is used to select , His representative work is win32 API Of
WndProc()function :
LRESULT CALLBACK WndProc(HWND hWnd,
UINT message,
WPARAM wParam,
LPARAM lParam)
In this function , We need to use switch sentence , choice message The type of the parameter , The typical code is :
switch(message)
{
case WM_PAINT:
// ...
break;
case WM_DESTROY:
// ...
break;
...
}
- Each event corresponds to an event handler .Qt That's the mechanism used :
mouseEvent()keyPressEvent()- …
Qt There are so many event handling functions , There must be a place to distribute it , otherwise ,Qt How do you know which event handler is called by which event ? This distributed function , Namely event(). obviously , When QMouseEvent After that ,event() Function to distribute it to mouseEvent() Event handler .
event() Functions have two problems :
event()Function is a protected Function of , It means that we want to rewriteevent(), Must inherit an existing class . Just imagine , My program doesn't want mouse events at all , All components in the program are not allowed to handle mouse events , Do I have to inherit all the components , Rewrite them one by oneevent()function ?protected Another problem with functions is , If I develop based on third-party libraries , The other side did not provide the source code , There's only one link library , Everything else is packaged . How can I inherit the components in this library ?event()Functions do have some control , But sometimes my needs are more stringent : I hope those components don't see this kind of event at all .event()Function can intercept , But it's actually receivedQMouseEventobject . I can't even get it . The advantage of this is , Simulating a system has no effect at all on that event , So other components don't receive this event at all , You don't have to modify your own event handling functions . What about this demand ?
These two questions are event() Functions can't handle . therefore ,Qt Provides another solution : Event filter . Event filters give us the ability to , Let's remove something completely . Event filters can be installed to any QObject Type above , And you can install more than one . If you want to implement a global event filter , Can be installed to QApplication perhaps QCoreApplication above . What needs to be noted here is , If you use installEventFilter() Function to install an event filter for an object , Then the event filter is only valid for that object , Only the events of this object need to be passed to the event filter first eventFilter() Function to filter , Other objects are not affected . If QApplication Object to install the event filter , So the filter works for every object in the program , The event of any object is first passed to eventFilter() function .
Event filter can solve the problem we just proposed event() Two shortcomings of the function : First , Event filters are not protected Of , So we can offer any QObject Subclasses install event filters ; secondly , The event filter processes the event before it is received by the target object , If we filter out events , The target will not see this event at all .
in fact , There's another way , We didn't introduce .Qt The invocation of events will eventually go back to QCoreApplication::notify() function , therefore , The biggest control is actually rewriting QCoreApplication::notify(). The declaration of this function is :
virtual bool QCoreApplication::notify ( QObject * receiver, QEvent * event );
This function will event Send to receiver, That is to call receiver->event(event), The return value is from receiver Event handler for . Be careful , This function is called for any event of any object in any thread , therefore , It doesn't have a thread problem with event filters . But we don't recommend that , because notify() There is only one function , And event filters are much more flexible .
Now we can summarize Qt Event handling , There are actually five levels :
- rewrite
paintEvent()、mousePressEvent()And so on . This is the most common 、 The simplest form , At the same time, the function is the simplest . - rewrite
event()function .event()Function is the event entry for all objects ,QObjectandQWidgetIn the implementation of , The default is to pass events to a specific event handler . - Install event filters on specific objects . The filter filters only the events received by the object .
- stay
QCoreApplication::instance()Event filters are installed on it . This filter will filter all events for all objects , So withnotify()Functions are just as powerful , But it's more flexible , Because you can install multiple filters . The global event filter can see disabled Mouse events emitted from components . There's a problem with global filters : Can only be used on the main thread . - rewrite
QCoreApplication::notify()function . This is the most powerful , Like the global event filter, it provides full control , And it's not limited by threads . But only one can be used globally ( becauseQCoreApplicationIs a singleton ).
In order to further understand the calling sequence of these levels of event processing , We can write a test code :
class Label : public QWidget
{
public:
Label()
{
installEventFilter(this);
}
bool eventFilter(QObject *watched, QEvent *event)
{
if (watched == this) {
if (event->type() == QEvent::MouseButtonPress) {
qDebug() << "eventFilter";
}
}
return false;
}
protected:
void mousePressEvent(QMouseEvent *)
{
qDebug() << "mousePressEvent";
}
bool event(QEvent *e)
{
if (e->type() == QEvent::MouseButtonPress) {
qDebug() << "event";
}
return QWidget::event(e);
}
};
class EventFilter : public QObject
{
public:
EventFilter(QObject *watched, QObject *parent = 0) :
QObject(parent),
m_watched(watched)
{
}
bool eventFilter(QObject *watched, QEvent *event)
{
if (watched == m_watched) {
if (event->type() == QEvent::MouseButtonPress) {
qDebug() << "QApplication::eventFilter";
}
}
return false;
}
private:
QObject *m_watched;
};
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
Label label;
app.installEventFilter(new EventFilter(&label, &label));
label.show();
return app.exec();
}
We can see , The output result after mouse click is :
QApplication::eventFilter
eventFilter
event
mousePressEvent
So you know , The global event filter is called first , Then there is the event filter on the object , The second is event() function , Finally, there are specific event handlers .
边栏推荐
- Photoshop提示暂存盘已满怎么办?ps暂存盘已满如何解决?
- 利用Power Automate,轻松下载Power BI报告中的数据
- Counting Nodes in a Binary Search Tree
- Photoshop裁剪工具隐藏技巧
- TCP three handshakes and four disconnects
- Digital integrated circuit: MOS tube device chapter (I)
- Replication of df-gan experiment -- detailed steps of replication of dfgan and forwarding from remote port to local port using mobaxtem
- "Photoshop2021 introductory tutorial" flatten "perspective images
- Hiding skills of Photoshop clipping tool
- Final Cut Pro中文教程 (2) 素材窗口的认识
猜你喜欢

Comprehensive experiment of static routing

STL upper series - detailed explanation of list container

树莓派输出PWM波驱动舵机

How do I reset Photoshop preferences? PS method of resetting preferences

Solution: how to use bash batch command in win10

【搜索】Flood Fill 和 最短路模型

How to copy Photoshop layers to other documents
![[error reporting]: cannot read properties of undefined (reading 'prototype')](/img/e4/528480610b9eed6028d7b3b87571e2.png)
[error reporting]: cannot read properties of undefined (reading 'prototype')

STM32 Hal serial port (uart/usart) debugging experience (I) -- basic knowledge of serial port communication +hal library code understanding

Final Cut Pro Chinese tutorial (2) understanding of material window
随机推荐
「Photoshop2021入门教程」“拉平”带有透视感的图像
Hiding skills of Photoshop clipping tool
【搜索】—— 多源BFS + 最小步数模型
[untitled] I is circularly accumulated under certain conditions. The condition is usually the length of the loop array. When it exceeds the length, the loop will stop. Because the object cannot judge
What about PS too laggy? A few steps to help you solve the problem
文件对话框
MySQL下载安装 & 完美卸载
Photoshop各历史版本回顾以及系统要求
HCIA dynamic routing OSPF experiment
How to import PS style? Photoshop style import tutorial
节流函数的demo——正则表达式匹配
Dynamic routing configuration
Photoshop裁剪工具隐藏技巧
[C language] detailed explanation of user-defined types (structure + enumeration + Union)
Hiding skills of Photoshop clipping tool
「Photoshop2021入门教程」对齐与分布制作波点图案
strlen和sizeof的区别
Complete Binary Tree
Photoshop提示暂存盘已满怎么办?ps暂存盘已满如何解决?
不需手动安装cuda和cudnn,通过一行程序即可安装tensorflow-gpu,以tensorflow-gpu2.0.0,cuda10.0,cudnn7.6.5为例