当前位置:网站首页>QT learning 16 parent-child relationships between QT objects
QT learning 16 parent-child relationships between QT objects
2022-06-29 11:35:00 【A little black sauce】
Qt Study 16 Qt The parent-child relationship between objects
problem
In the last section QCalculatorUI Class object Create... In heap space A text box and a button , but There is no counterpart delete Code , Whether it is a memory leak ?
Qt The relationship between objects
- Qt Objects can There's a father son relationship
- Every object is preservation Have it Pointers to all child objects
- Every object has a Pointer to its parent object

- When specifying Qt When the parent object of the object
- Its parent object will add the pointer of the object to the linked list of child objects
- The object holds a pointer to its parent object
QObject* p = new QObject();
QObject* c1 = new QObject();
QObject* c2 = new QObject();
c1->setParent(p);
c2->setParent(p);
Code experiments
#include <QCoreApplication>
#include <QDebug>
void fcTest()
{
QObject* p = new QObject();
QObject* c1 = new QObject();
QObject* c2 = new QObject();
c1->setParent(p);
c2->setParent(p);
qDebug() << "c1: " << c1; // c1: QObject(0x767550)
qDebug() << "c2: " << c2; // c2: QObject(0x7675a8)
const QObjectList& list = p->children();
for (int i = 0; i < list.length(); i++) {
qDebug() << list[i]; // QObject(0x767550) QObject(0x7675a8)
}
qDebug() << "p: " << p; // p: QObject(0x7674f8)
qDebug() << "c1 parent: " << c1->parent(); // c1 parent: QObject(0x7674f8)
qDebug() << "c2 parent: " << c1->parent(); // c2 parent: QObject(0x7674f8)
}
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
fcTest();
return a.exec();
}
When Qt When an object is destroyed
- Remove yourself from the parent object's Children List remove
- To his own Children List Destroy all objects in
Use Qt When developing , Not only should we always pay attention to Memory leak The problem of , Also, always pay attention to whether the object can be Multiple destruction The problem of !
utilize Qt object The father son relationship Can form an object tree
Deleting a node in the tree will Cause the corresponding subtree to be destroyed

Code experiments
#include <QCoreApplication>
#include <QDebug>
class MObj : public QObject
{
QString m_name;
public:
MObj(const QString& name) {
m_name = name;
qDebug() << "Constructor: " << m_name;
}
~MObj() {
qDebug() << "Destructor: " << m_name;
}
};
void delTest()
{
MObj *obj1 = new MObj("obj1"); // Constructor: "obj1"
MObj *obj2 = new MObj("obj2"); // Constructor: "obj2"
MObj *obj3 = new MObj("obj3"); // Constructor: "obj3"
MObj *obj4 = new MObj("obj4"); // Constructor: "obj4"
obj2->setParent(obj1);
obj3->setParent(obj1);
obj4->setParent(obj3);
delete obj3; // Destructor: "obj3" Destructor: "obj4"
const QObjectList& list = obj1->children();
qDebug() << "obj2: " << obj2; // obj2: QObject(0x107a920)
for (int i = 0; i < list.length(); i++) {
qDebug() << list[i]; // QObject(0x107a920)
}
}
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
delTest();
return a.exec();
}
Summary
- Qt Objects can exist Father and son
- Through a father son relationship Can get Qt Object tree
- Qt object Remove when destroyed And the parent object Father and son
- Qt object At the time of destruction The colleague destroys all child objects
边栏推荐
- [daily 3 questions (3)] reformat the phone number
- Multithreaded high concurrency server: three problems
- Doodle cloud development demo login
- (JS) array de duplication
- matlab基础 max 求一维或二维数组的最大值+sleep(pause)
- Modbustcp protocol network learning single channel infrared module (double-layer board)
- Shell 中你不得不熟知的变量运用
- How to test the performance of container platform, including stability, expansion efficiency and component performance
- misc3~7
- (JS) handwriting depth comparison
猜你喜欢

什么?漫画居然能免费看全本了,这还不学起来一起做省钱小能手

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

云原生开发必备:首个通用无代码开发平台 iVX 编辑器

Pipeline aggregations pipeline aggregations - parent-2

Qt学习09 计算器界面代码重构

Xuetong denies that the theft of QQ number is related to it: it has been reported; IPhone 14 is ready for mass production: four models are launched simultaneously; Simple and elegant software has long

巴比特 | 元宇宙每日必读:HTC 宣布推出首款元宇宙手机,售价约2700元人民币,都有哪些新玩法?...

Easydss is deployed on Disk C, and the video playback cannot be played normally. How to solve this problem?

(JS) filter out keys with value greater than 2 in the object

又拍云 Redis 的改进之路
随机推荐
Pipeline aggregations管道聚合-Sibling-1
The Chinese Computational Linguistics Conference and the national knowledge atlas and Semantic Computing Conference are in full swing
ModbusTCP协议WIFI无线学习型单路红外模块(小壳版)
Uboot for embedded driver development -- common command parameters in uboot
How to identify the exact length and width of the contour
The former security director of Uber faced fraud allegations and concealed the data leakage event
极限导论总结
Shell quotation marks and escape are rarely noticed, but they are often used in writing scripts
Limit introduction summary
Specific method and example program of Siemens s7-200smart control stepping motor
Qt学习09 计算器界面代码重构
Bs-gx-017 online examination management system based on SSM
Qt学习11 Qt 中的字符串类
【无标题】我在密谋一件大事
ModbusTCP协议网络学习型单路红外模块(中壳版)
TTL串口学习型红外遥控模块可扩展成网络控制
Ikvm Net project progress
Nuc980 open source project 16- start from SPI flash (w25q128)
数据分析方法与思维:漏斗分析
[3 questions per day (2)] minimum operand for generating alternate binary strings