当前位置:网站首页>Qt学习16 Qt 对象间的父子关系
Qt学习16 Qt 对象间的父子关系
2022-06-29 10:30:00 【一个小黑酱】
Qt学习16 Qt 对象间的父子关系
问题
上一节中的QCalculatorUI类对象在堆空间中创建了文本框和按钮,但没有对应的delete代码,是否为内存泄漏?
Qt对象间的关系
- Qt对象间可以存在父子关系
- 每一个对象都保存有它所有子对象的指针
- 每一个对象都有一个指向其父对象的指针

- 当指定Qt对象的父对象时
- 其父对象会在子对象链表中加入该对象的指针
- 该对象会保存指向其父对象的指针
QObject* p = new QObject();
QObject* c1 = new QObject();
QObject* c2 = new QObject();
c1->setParent(p);
c2->setParent(p);
代码实验
#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();
}
当Qt对象被销毁时
- 将自己从父对象的Children List移除
- 将自己的Children List中的所有对象销毁
使用Qt开发时,不仅要时刻注意内存泄漏的问题,还要时刻关注对象是否可能被多次销毁的问题!
利用Qt对象间的父子关系可以构成对象树
删除树中的节点会导致对应的子树被销毁

代码实验
#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();
}
小结
- Qt对象间可以存在父子关系
- 通过父子关系能够得到Qt对象树
- Qt对象销毁时解除和父对象间的父子关系
- Qt对象销毁时同事销毁所有的子对象
边栏推荐
猜你喜欢

【各种**问题系列】OLTP和OLAP是啥?

Bs-gx-018 student examination system based on SSM

ruoyi框架中添加sharding sphere5.0.0分表(通过spi添加自定义分表策略)

Multi thread communication between client and server (primary version)

Interview questions of Tencent automation software test of CSDN salary increase secret script (including answers)

Nature | 全球海洋微生物组的生物合成潜力

EasyDSS部署在C盘,录像回看无法正常播放该如何解决?

Online text filter less than specified length tool

What is the experience of working in an IT company in Japan?

Google Earth Engine(GEE)——GEDI L2A Vector Canopy Top Height (Version 2) 全球生态系统数据集
随机推荐
Lizuofan, co-founder of nonconvex: Taking quantification as his lifelong career
9 easy to use JSON processing tools, greatly improving efficiency!
【无标题】我在密谋一件大事
Getting started with the lvgl Library - Animation
NUC980开源项目16-从SPI FLASH(W25Q128)启动
The use of variables in shell that you have to be familiar with
Doodle cloud development demo login
(JS) array methods: slice and slice
非凸联合创始人李佐凡:将量化作为自己的终身事业
Encore une fois, le chemin de l'amélioration de redis Cloud
Pipeline aggregations pipeline aggregations - parent-2
Evaluation of IP location query interface Ⅱ
中科方德技术专家直播:如何基于 OpenStack、Ceph 构建私有云平台? | 第 27 期
Modbustcp protocol network learning single channel infrared module (medium shell version)
Hit the industry directly! The first model selection tool in the industry was launched by the flying propeller
云原生开发必备:首个通用无代码开发平台 iVX 编辑器
Pull and push ideas behind rxjs observable design principles
map合并相同的键,值合并为列表
Course design for the end of the semester: product sales management system based on SSM
[untitled] I am plotting a big event