当前位置:网站首页>Custom events
Custom events
2022-07-28 01:39: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);
}边栏推荐
猜你喜欢

MATLAB 44种动漫渐变色绘图程序

Unknown database ‘xxxxx‘

Summary of common shortcut keys in idea

Thoroughly understand kubernetes scheduling framework and plug-ins

8000字讲透OBSA原理与应用实践

Storage practices for high-performance computing scenarios, see here

Flutter 通话界面UI

Unity Shader入门精要学习——基础纹理

LeetCode 2341. 数组能形成多少数对

LeetCode 2351. 第一个出现两次的字母
随机推荐
JG-数据重置(wd)
Icml2022 | online decision transformer
华为“天才少年”稚晖君又出新作,从零开始造“客制化”智能键盘
MySQL JPA support for JSON type data in database
Can ordinary equipment access TSN time sensitive network?
LeetCode 2341. 数组能形成多少数对
Shaanxi Yuanjie semiconductor, a laser chip manufacturer, was invested by 8 investment institutions including GF Securities and CITIC Securities
还在用WIFI你就OUT了:LI-FI更牛!!!
Flutter--密码登录注册界面
Lua get started quickly
Introduction to routing strategy
Support Wu xiongang! Arm Chinese management sent an open letter: shocked and angry at the unwarranted allegations!
面试题 01.09. 字符串轮转
Advanced MySQL -- stored procedures and custom functions
Gossip: an initially perfect FS is as impractical as the first version of the program requiring no bugs
Huawei responded to the US blockade of the supply chain: they still have to pay for 5g patents
Leetcode 2347. the best poker hand
Baidu PaddlePaddle easydl: when AI enters the factory, "small bearing" can also turn "big industry"
Software testing interview question: where do your performance testing requirements come from?
Three instance