当前位置:网站首页>Event handling in QT
Event handling in QT
2022-06-30 12:48:00 【AlbertOS】
introduce
Events are not signals and slots ;
These two concepts are easily confused , The essence of the signal and slot is Qt A processing mechanism for handling events , We can't confuse it .
In fact, there are many different event handling mechanisms , for example js The event binding mechanism used ,windows Message mechanism adopted , For details, please see my other blogs windows Message mechanism learning
What was the event ?
An event is a thing , Or it is an operation behavior , The user moves the mouse 、 The click of a mouse 、 All operations such as keyboard tapping are called events ( The system can also emit events , Such as timer events )
The specific implementation of the event
The implementation of events is implemented through the subscription publishing mode , That is, pre-defined events in the code run , This predefined event is triggered in a later operation , Execute the event handler ( You can have a look at the specific source code windows Official documents of )
stay Qt In, we use chain processing to handle events , And the browser DOM2 The event model is consistent , There are three processes :
- Event capture phase ( From top to bottom )
Capture refers to the event from Windows The object propagates down to the target element , Check in turn whether the nodes passing by are bound with event listeners , If so, execute , Otherwise - At the target stage
Also known as the event processing phase , This stage executes the listening event of the target element binding - Event bubbling stage ( Bottom up )
Bubbling refers to the event bubbling from the target element to Windows object , Check in turn whether the nodes passing by are bound with event listeners
Why do we have to deal with all the incidents ? Let's take a look at a code to understand !
void MyLabel::mousePressEvent(QMouseEvent * event)
{
if(event->button() == Qt::LeftButton) {
// do something
} else {
QLabel::mousePressEvent(event);
}
}
We overridden the event function in the subclass , Detect... In mouse down events , If you press the left button , Do our processing work , If it's not the left button , Call the function of the parent class , It can be said that the event is passed up to the parent class to respond , That is, we ignore this event in the subclass, which causes the event to bubble
We can Qt The transmission of events is regarded as the responsibility chain structure , If the subclass does not find the event handler in the event capture phase , Will continue to pass... To other classes
( Actually ,Qt All event objects have a accept() Functions and ignore() function . Just like their name , The former is used to tell Qt, Event handler “ receive ” The incident happened , Don't pass ; The latter tells Qt, Event handler “ Ignore ” The incident happened , Need to continue to pass , Find another recipient . In the event handler , have access to isAccepted() To check whether this event has been received )
We seldom use accept() and ignore() function , But like the example above , If you want to ignore Events , Just call the response function of the parent class . Remember we said ,Qt Most of the events in are protected Of , therefore , The overridden function must have a response function in its parent class , This method is feasible . Why do that ? Because we can't confirm that the handler in the parent class has no operation , If we ignore events directly in subclasses ,Qt No more looking for other recipients , Then the operation of the parent class cannot be carried out , This can be potentially dangerous .
But there is a situation , We have to use it, too accept() and ignore() function , When the window closes , If you want to have a query dialog box, you must actively use accept() Function to actively accept the message ( You can also think of him as a kind of Hook Mechanism , It will be sent to QObject The message of was actively intercepted , Used to deal with their own work )
void MainWindow::closeEvent(QCloseEvent * event)
{
if(continueToClose()) {
event->accept();
} else {
event->ignore();
}
}
bool MainWindow::continueToClose()
{
if(QMessageBox::question(this,
tr(" sign out "),
tr(" Are you sure you want to close the window ?"),
QMessageBox::Yes | QMessageBox::No,
QMessageBox::No)
== QMessageBox::Yes) {
return true;
} else {
return false;
}
}
Reference resources
We can also define events from the perspective of web programming :
What was the event ? Event model ?
Browsers can also directly handle DOM0 event ,Qt Because of existence Windows Classes and Qtapplication Class interaction , I don't know if I can go directly to Qt Window DOM Write the event handler directly in the
边栏推荐
- Substrate 源码追新导读: 修复BEEFY的gossip引擎内存泄漏问题, 智能合约删除队列优化
- Illustration creating a stored procedure using Navicat for MySQL
- Pharmacy management system
- 电机控制park变换公式推导
- Introduction to sub source code updating: mid May: uniques NFT module and nomination pool
- 【一天学awk】内置变量的使用
- 时空预测2-GCN_LSTM
- [learn awk in one day] operator
- Solve numpy core._ exceptions. Ufunctypeerror: UFUNC 'Add' did not contain a loop with signature matching
- Charles break point modify request data & response data
猜你喜欢

Sarsa notes

Analysis of the whole process of common tilt data processing in SuperMap idesktop

黑马笔记---集合(Collection的常用方法与遍历方式)

Apple executives openly "open the connection": Samsung copied the iPhone and only added a large screen

MySQL built-in functions
![[surprised] the download speed of Xunlei is not as fast as that of the virtual machine](/img/04/b0d23266b70c9ad6990a5203ef8ddf.png)
[surprised] the download speed of Xunlei is not as fast as that of the virtual machine

Wechat launched the picture big bang function; Apple's self-developed 5g chip may have failed; Microsoft solves the bug that causes edge to stop responding | geek headlines

zabbix-server启动失败处理方式

Efficient elliptic curve point addition and multiplication in scrypt
![[one day learning awk] array usage](/img/75/4333452142a3c7325e0712f3306985.png)
[one day learning awk] array usage
随机推荐
第十三章 信号(三)- 示例演示
[QNX Hypervisor 2.2用户手册]6.2.3 Guest与外部之间通信
Vision based robot grasping: from object localization, object pose estimation to parallel gripper grasping estimation
Reading the table data of Tencent documents in the applet
Clipboardjs - development learning summary 1
How to use AI technology to optimize the independent station customer service system? Listen to the experts!
【OpenGL】OpenGL Examples
Browser plays RTSP video based on nodejs
How to solve cross domain problems
Sarsa notes
图解使用Navicat for MySQL创建存储过程
Flink sql控制台,不识别group_concat函数吗?
【一天学awk】运算符
dataworks 同步maxcomputer 到sqlserver ,汉字变乱码了,请问怎么解决
Analysis of the whole process of common tilt data processing in SuperMap idesktop
[one day learning awk] array usage
Illustration creating a stored procedure using Navicat for MySQL
Redis-缓存问题
zabbix-server启动失败处理方式
STM32 移植 RT-Thread 标准版的 FinSH 组件