当前位置:网站首页>Custom events
Custom events
2022-07-29 07:13:00 【PHP code】
Even though Qt A lot of events have been provided , But for more diverse needs , Limited events are not enough . for example , I want to support a new device , This device provides a new way of interaction , that , How to deal with this kind of incident ? therefore , Allow to create your own events Type is inevitable . Even if we don't say that kind of extreme example , In a multithreaded program , Custom events are also particularly useful . Of course , Events are not limited to multithreading , It can be used in single threaded programs , As a communication mechanism between objects . that , Why do I need to use events , Not the signal slot ? The main reason is , The distribution of events can be synchronous , It can also be asynchronous , Function calls or slot callbacks are always synchronized . Another benefit of the event is , It can use filters .
Qt Custom events are simple , It's very similar to the use of other class libraries , They all need to inherit a class for extension . stay Qt in , The classes you need to inherit are QEvent.
Inherit QEvent class , The most important thing is to provide a QEvent::Type Parameters of type , As the type value of custom event . recall , This type It is the code we use to identify the event type when dealing with events . For example event() Function , We use QEvent::type() Get this event type , Then compare it with the actual type we define .
QEvent::Type yes QEvent An enumeration defined . therefore , We can pass on a int value . But here's the thing , Our custom event types cannot be compared with existing type Value is repeated , Otherwise, unexpected errors will occur . Because the system will send and call your newly added events as system events . stay Qt in , System reservation 0 – 999 Value , in other words , Your event type Be greater than 999. Of course, this kind of value is very difficult to remember , therefore Qt Two boundary values are defined :QEvent::User and QEvent::MaxUser. Our custom events type It should be between the ranges of these two values . among ,QEvent::User The value of is 1000,QEvent::MaxUser The value of is 65535. I know from here , We can at most define 64536 Events . Through these two enumeration values , We can ensure that our own event types will not cover the event types defined by the system . however , This does not guarantee that custom events will not be overwritten with each other . To solve this problem ,Qt Provides a function :registerEventType(), Registration for custom events . The signature of this function is as follows :
static int QEvent::registerEventType ( int hint = -1 );
This function is static Of , So you can use QEvent Class directly calls . The function accepts a int value , The default value is -1; The return value of the function is a new value registered with the system Type Type value . If hint It's legal. , That is to say hint No overrides will occur ( System and other custom events ), This value will be returned directly ; otherwise , The system will automatically assign a legal value and return . therefore , Use this function to complete type Value assignment . This function is thread safe , There is no need to add additional synchronization .
We can do it in QEvent Add the data required by your own event in the subclass , Then send the event .Qt Two event sending methods are provided in :
static bool QCoreApplication::sendEvent(QObject *receiver,
QEvent *event);
Direct will event The event is sent to receiver The recipient , It uses QCoreApplication::notify() function . The return value of the function is the return value of the event handler function . When the event is sent ,event Objects are not destroyed . Usually we will create on the stack event object , for example :
QMouseEvent event(QEvent::MouseButtonPress, pos, 0, 0, 0);
QApplication::sendEvent(mainWindow, &event);
static void QCoreApplication::postEvent(QObject *receiver,
QEvent *event);
take event Events and their recipients receiver Add to the event queue together , The function immediately returns .
because post The event queue holds the event object , And in it post When you need it delete fall , therefore , We have to create on the heap event object . When the object is sent , Then try to access event Objects will have problems ( because post after ,event The object will be delete).
When control returns to the main thread, the loop is , All events saved in the event queue pass through notify() Function to send out .
The event will be based on post Process in the order of . If you want to change the order of events , Consider giving it a priority . The default priority is Qt::NormalEventPriority.
This function is thread safe .
Qt There's also a function :
static void QCoreApplication::sendPostedEvents(QObject *receiver,
int event_type);
The function is , The receiver in the event queue is receiver, The event is similar to event_type All events of are immediately sent to receiver To deal with . It should be noted that , Events from the window system are not handled by this function , It is processEvent(). Please refer to Qt API manual .
Now? , We have been able to customize the event object , It has been able to send events , There is one last step left : Handle custom events . Handle custom events , There is no difference with the treatment methods we explained above . We can rewrite QObject::customEvent() function , This function receives a QEvent Object as parameter :
void QObject::customEvent(QEvent *event);
We can transform event Object type to judge different events :
void CustomWidget::customEvent(QEvent *event) {
CustomEvent *customEvent = static_cast<CustomEvent *>(event);
// ...
}
Of course , We can also do that event() Directly handle... In function :
bool CustomWidget::event(QEvent *event) {
if (event->type() == MyCustomEventType) {
CustomEvent *myEvent = static_cast<CustomEvent *>(event);
// processing...
return true;
}
return QWidget::event(event);
}
边栏推荐
- 【C语言刷LeetCode】67. 二进制求和(E)
- Win11 system error: code execution cannot continue because ierutil.dll cannot be found. Reinstalling the program may fix this problem
- Basic knowledge of MySQL (high frequency interview questions)
- dba
- [cf1054h] epic Revolution -- number theory, convolution, arbitrary modulus NTT
- Dbasql interview questions
- 【C语言刷LeetCode】1054. 距离相等的条形码(M)
- H3C_利用设置缺省静态路由优先级实现出口双线路的主备功能
- ECCV 2022 lightweight model frame Parc net press apple mobilevit code and paper Download
- Simulation volume leetcode [normal] 081. Search rotation sort array II
猜你喜欢
后缀自动机(SAM)讲解 + Luogu p3804【模板】后缀自动机 (SAM)
Thread - thread safety - thread optimization
使用VsCode配置MySQL实现连接、查询、等功能
Win11 system error: code execution cannot continue because ierutil.dll cannot be found. Reinstalling the program may fix this problem
以太网接口介绍
Implementation of book borrowing management system based on C language
win11VMware打开虚拟机就蓝屏重启以及启动不了的解决方案
ECCV 2022 lightweight model frame Parc net press apple mobilevit code and paper Download
【charles日常问题】开启charles,使用不了钉钉
约瑟夫环问题
随机推荐
后缀自动机(SAM)讲解 + Luogu p3804【模板】后缀自动机 (SAM)
Vite3.0都发布了,你还能卷得动吗(新特性一览)
Cesium reflection
ETL为什么经常变成ELT甚至LET?
Windows 上 php 7.4 连接 oracle 配置
怎么会不喜欢呢,CICD中轻松发送邮件
微信小程序的反编译
记 - 踩坑-实时数仓开发 - doris/pg/flink
约瑟夫环问题
WPF简单登录页面的完成案例
Improved pillar with fine grained feature for 3D object detection paper notes
Unity exploration plot access design analysis & process + code specific implementation
如何使用gs_expansion扩展节点
做开发4年13K,想转行自动化测试,薪资还能涨吗···
CAN&CANFD综合测试分析软件LKMaster与PCAN-Explorer 6分析软件的优势对比
buck电路boot和ph引脚实测
vim文本编辑器的一些使用小技巧
Use vscode to configure Mysql to realize connection, query, and other functions
JS 鸡生蛋与蛋生鸡问题,Object与Function究竟谁出现的更早?Function算不算Function的实例?
Win11vmware turns on the virtual machine and restarts on the blue screen and the solution that cannot be started