当前位置:网站首页>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);
}边栏推荐
- Improved pillar with fine grained feature for 3D object detection paper notes
- 谷歌零碎笔记之JWT(草稿)
- Simulation volume leetcode [normal] 081. Search rotation sort array II
- [C language brush leetcode] 1054. Bar code with equal distance (m)
- Excel file reading and writing (creation and parsing)
- 2D cartoon rendering - advanced skills
- 【C语言刷LeetCode】2332. 坐上公交的最晚时间(M)
- Hj37 statistics of the total number of rabbits per month Fibonacci series
- [Charles' daily problems] when you open Charles, you can't use nails
- 基于C语言实现图书借阅管理系统
猜你喜欢

记 - 踩坑-实时数仓开发 - doris/pg/flink

微信小程序的反编译

WPF嵌套布局案例

Image noise and matrix inversion

Idea cannot find a database solution

2022-07-28:以下go语言代码输出什么?A:AA;B:AB;C:BA;D:BB。 package main import ( “fmt“ ) func main() { f

以太网接口介绍

VMware16创建虚拟机:无法创建新虚拟机,不具备执行此操作的权限

Problems encountered in vmware16 installing virtual machines

Vmware16 create virtual machine: win11 cannot be installed
随机推荐
聊天机器人有何用处?有何类型?看完这些就明白了!
VMware16创建虚拟机:Win11无法安装
网上传说软件测试培训真的那么黑心吗?都是骗局?
Win11vmware turns on the virtual machine and restarts on the blue screen and the solution that cannot be started
MySQL 有这一篇就够(呕心狂敲37k字,只为博君一点赞!!!)
Simulation volume leetcode [normal] 081. Search rotation sort array II
buck电路boot和ph引脚实测
WPF嵌套布局案例
Analog volume leetcode [normal] 093. Restore IP address
vagrant box 集群 处理
数组的子集不能累加出的最小正数
win11VMware打开虚拟机就蓝屏重启以及启动不了的解决方案
Relative date used by filter in salesforce
Flink real-time warehouse DWD layer (transaction domain - additional purchase dimension degradation processing) template code
谷歌零碎笔记之JWT(草稿)
图像加噪声与矩阵求逆
【C语言刷LeetCode】2332. 坐上公交的最晚时间(M)
[Charles' daily problems] when you open Charles, you can't use nails
Implementation of book borrowing management system based on C language
记 - 踩坑-实时数仓开发 - doris/pg/flink