当前位置:网站首页>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 .
边栏推荐
- Counting Nodes in a Binary Search Tree
- Complete Binary Tree
- 使用Photoshop出现提示“脚本错误-50出现一般Photoshop错误“
- ps太卡怎么办?几步帮您解决问题
- MySQL下载安装 & 完美卸载
- 一道数学题,让芯片巨头亏了5亿美金
- 文件对话框
- [error reporting] cannot read property 'parsecomponent' of undefined
- Simple static routing in ENSP
- Row, table, page, share, exclusive, pessimistic, optimistic, deadlock
猜你喜欢

Final Cut Pro中文教程 (1) 基础认识Final Cut Pro

Reproduce ssa-gan using the nine day deep learning platform

多态的详讲

Transaction database and its four characteristics, principle, isolation level, dirty read, unreal read, non repeatable read?

如何做数据平滑迁移:双写方案

Hiding skills of Photoshop clipping tool
![[C language] detailed explanation of user-defined types (structure + enumeration + Union)](/img/d9/b10371159c63c126b5ff98bac0971a.png)
[C language] detailed explanation of user-defined types (structure + enumeration + Union)

Review of various historical versions of Photoshop and system requirements

Basic configuration of static routing to achieve network wide accessibility

SVN使用详解
随机推荐
How does PS import LUT presets? Photoshop import LUT color preset tutorial
Easily download data in power Bi reports with power auto
Simple static routing in ENSP
Mysql表的约束
测试基础5
JS tips
TypeScript 详解
Plane conversion (displacement, rotation, scaling)
R-score reproduction R-Precision evaluation index quantitative text generation image r-score quantitative experiment whole process reproduction (R-Precision) quantitative evaluation experiment step on
如何将Photoshop图层复制到其他文档
[C language] detailed explanation of user-defined types (structure + enumeration + Union)
项目对接支付宝支付,内网穿透实现监听支付宝的支付成功异步回调通知
When using Photoshop, the prompt "script error -50 general Photoshop error appears“
ps怎么导入lut预设?Photoshop导入lut调色预设教程
Plato Farm有望通过Elephant Swap,进一步向外拓展生态
[search] - multi source BFS + minimum step model
Plato farm is expected to further expand its ecosystem through elephant swap
[search] two way search + A*
[search] connectivity model of DFS + search order
不需手动安装cuda和cudnn,通过一行程序即可安装tensorflow-gpu,以tensorflow-gpu2.0.0,cuda10.0,cudnn7.6.5为例