当前位置:网站首页>Discussion on QT learning 10 message processing in QT
Discussion on QT learning 10 message processing in QT
2022-06-29 11:35:00 【A little black sauce】
Qt Study 10 On Qt Message processing in
Qt Message model
- Qt encapsulation Of the specific operating system Message mechanism
- Qt follow classical GUI Message driven time model

reflection
- Qt in How to represent user messages
- Qt in How to map user messages to message handling functions ?
- Qt in What rules do message mappings need to follow ?
Signal and slot
Qt Concepts related to system messages are defined in
The signal ( Signal )
- Messages generated by the operating system
Slot ( Slot )
- Message handling functions in programs
Connect ( Connect )
- Bind system messages to message handling functions
Qt Message handling mechanism in

The signal to slot connection must occur in two Qt Between class objects
- Qt Of The core - QObject::connect function
bool connect(const QObject* sender, // Send to
const char* signal, // Message name
const QObject* receiver, // The recipient
const char* method, // Accept member functions of objects
Qt::ConnectionType type=Qt::AutoConnection)
remarks : stay Qt in , For message character string Describe ;connect Function in Message name and Processing function Between Building mapping
Qt Medium “ new ” keyword
SIGNAL
- Used to specify the message name
SLOT
- Used to specify the name of the message handling function 1
Q_OBJECT
- All custom slot classes You must add... At the beginning of the class declaration Q_OBJECT
slot
- Used in classes Declare the message handling function
Custom slot
- Only OBject Subclass of can Custom slot
- The class that defines the operation must be in At the beginning of the declaration, use Q_OBJECT
- Class Declare slot When needed slots keyword
- Slot And the The signal stay Function signatures must be consistent
- SIGNAL and SLOT In the specified name :
- Can contain parameter types
- Cannot contain specific parameter names
- Slot function through sender() function You can get the object of the signal
Calculator program plus signal slot
The following code contains only the signals and slots added , The previously repeated part is no longer shown
/* QCalculatorUI.h */
// The header file included in the previous code
class QCalculatorUI : public QWidget
{
Q_OBJECT // Use the configuration that must be added to the signal slot
private:
// Content in previous code
private slots: // Custom slot function
void onButtonClicked();
public:
// Content in previous code
};
/* QCalculatorUI.cpp */
// Content in previous code
bool QCalculatorUI::construct()
{
// Content in previous code
for(int i=0; (i<4) && ret; i++) {
for(int j=0; (j<5) && ret; j++) {
m_buttons[i*5 + j] = new QPushButton(this);
if( m_buttons[i*5 + j] != NULL ) {
// Content in previous code
connect(m_buttons[i*5 + j], SIGNAL(clicked()), this, SLOT(onButtonClicked())); // Add a slot function to the click event of each button
}
else {
ret = false;
}
}
}
return ret;
}
// Button click event custom slot function
void QCalculatorUI::onButtonClicked()
{
QPushButton* btn = (QPushButton*)sender(); // adopt sender() The function knows which button was pressed
qDebug() << "onButtonClicked()";
qDebug() << btn->text();
}
// Content in previous code
Tips:
Solve classic problems :Object::connect: No such slot …
1. Inspection class Whether to inherit from QObject
2. Check the beginning of the class declaration Whether to add Q_OBJECT
3. Check Whether to use slots Keyword for slot declaration
4. Check Whether the slot name is wrong
5. Re execution qmake
Summary
- Signal and slot yes Qt The core mechanism of the system
- Different Qt Objects can communicate through signals and slots
- Only QObject Subclass of can Custom signals and slots
- Signals and slots must be used in At the beginning of the declaration, use Q_OBJECT
- The signal With the handler in Function signatures must be consistent
边栏推荐
- 影响LED封装散热主要因素有哪些?
- 高效远程办公的基石:有效沟通 |社区征文
- Exclusive interview with head of suss NIFT: the future of Web3 is inseparable from the governance of "everyone for me, I for everyone"
- 【HBZ分享】Mysql的InnoDB原理
- Today in history: musk was born; Microsoft launches office 365; The inventor of Chua's circuit was born
- Shell 中你不得不熟知的变量运用
- XML外部实体注入漏洞(一)
- Lizuofan, co-founder of nonconvex: Taking quantification as his lifelong career
- Multithreaded high concurrency server: three problems
- 喜报|海泰方圆通过CMMI-3资质认证,研发能力获国际认可
猜你喜欢

Opencv4.0 installation of raspberry pie for Xiaobai

Live broadcast by technical experts from China Kuwait Fangde: how to build a private cloud platform based on openstack and CEPH| Issue 27

(JS) array de duplication

Multi thread communication between client and server (primary version)
![[various * * question series] what are OLTP and OLAP?](/img/3f/48b4108d14d40212f678971c1b62bb.png)
[various * * question series] what are OLTP and OLAP?

Qt学习01 GUI程序原理分析

Design and implementation of IDS

XML外部实体注入漏洞(一)

又拍雲 Redis 的改進之路

【每日3题(1)】判断国际象棋棋盘中一个格子的颜色
随机推荐
Uboot for embedded driver development -- common command parameters in uboot
Cornerstone of efficient remote office: effective communication | community essay solicitation
math_ Mathematical expression & deformation of equation equation & accumulation of combined operation skills / means
【每日3题(3)】重新格式化电话号码
Modbus RTU protocol 485 learning 2-way infrared module
在日本的 IT 公司工作是怎样一番体验?
The use of Fibonacci sequence and bubble sort in C language
多线程实现客户端与服务端通信(初级版本)
Pull and push ideas behind rxjs observable design principles
MySQL 索引失效的几种类型以及解决方式
Several types of MySQL index invalidation and their solutions
彻头彻尾理解JVM系列之七:对象在分代模型中的流转过程是怎样的?
Getting started with the lvgl Library - Animation
中国计算语言学大会、全国知识图谱与语义计算大会赛题火热进行中
Pipeline aggregations管道聚合-Sibling-1
QT learning 11 string classes in QT
QT learning 08 set sail! First application instance
Take another picture of cloud redis' improvement path
what? It's amazing that you can read the whole comic book for free. You can't learn to be a money saver together
CTO专访:合见工软深化产品布局 加速国产EDA技术革新