当前位置:网站首页>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
边栏推荐
- QT implementation dynamic navigation bar
- Qt中的数据库使用
- 60 个神级 VS Code 插件!!
- [one day learning awk] use of built-in variables
- JMeter之事务控制器
- 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
- 【OpenGL】OpenGL Examples
- JMeter之性能测试流程及性能测试关注点
- MySQL implements the division of two query results
- Solve numpy core._ exceptions. Ufunctypeerror: UFUNC 'Add' did not contain a loop with signature matching
猜你喜欢
![[one day learning awk] use of built-in variables](/img/5b/bc1b91804e03dcfd3fe7eae8084eb4.png)
[one day learning awk] use of built-in variables

Determining the subject area of data warehouse construction

【OpenGL】OpenGL Examples

Commands for redis basic operations

SuperMap 3D SDKs_ Unity plug-in development - connect data services for SQL queries

Four ways for flinksql to customize udtf

MATLAB小技巧(22)矩阵分析--逐步回归

Analysis of smart jiangcai login in Jiangxi University of Finance and Economics

Tencent two sides: @bean and @component are used on the same class. What happens?

Redis - problèmes de cache
随机推荐
[one day learning awk] array usage
MySQL judges the calculation result and divides it by 100
Today in history: Microsoft acquires PowerPoint developers; SGI and MIPS merge
[qnx hypervisor 2.2 user manual]6.2.3 communication between guest and external
kubeedge的核心理念
Sublist3r error reporting solution
【目标跟踪】|pytracking 配置 win 编译prroi_pool.pyd
SQLSERVER 查询编码是 936 简体中文GBK,那我是写936 还是写GBK?
Substrate 源码追新导读: 波卡系波卡权重计算全面更新, Governance 2.0 版本的优化和调整
Charles break point modify request data & response data
STM32 移植 RT-Thread 标准版的 FinSH 组件
力扣之螺旋矩阵,一起旋转起来(都能看懂)
Mysql判断计算结果,除以100
[yitianxue awk] regular matching
问卷星问卷抓包分析
Analysis of smart jiangcai login in Jiangxi University of Finance and Economics
Grep match lookup
LeetCode_栈_中等_227.基本计算器 II(不含括号)
时空预测2-GCN_LSTM
NoSQL - redis configuration and optimization