当前位置:网站首页>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);
}边栏推荐
猜你喜欢
随机推荐
Personal blog system (with source code)
After three years of outsourcing, the salary of automatic testing after job hopping is twice that of the original. The secret is
聊天机器人有何用处?有何类型?看完这些就明白了!
谷歌零碎笔记之JWT(草稿)
win11VMware打开虚拟机就蓝屏重启以及启动不了的解决方案
实现改变一段文字的部分颜色效果
Vmware16 create virtual machine: win11 cannot be installed
Student achievement ranking system based on C language design
SSH password free login - two virtual machines establish password free channel two-way trust
Some tips of vim text editor
330. 按要求补齐数组
Decompilation of wechat applet
Overview of database system
Student status management system based on C language design
Thread synchronization - producers and consumers, tortoise and rabbit race, dual thread printing
Hj37 statistics of the total number of rabbits per month Fibonacci series
Improved pillar with fine grained feature for 3D object detection paper notes
Basic knowledge of MySQL (high frequency interview questions)
Connecting PHP 7.4 to Oracle configuration on Windows
Win11vmware turns on the virtual machine and restarts on the blue screen and the solution that cannot be started









