1. The relationship between Qt objects
(1) Parent-child relationship can exist between QT objects
① Each object holds a pointer to all its child objects
② Each object has a pointer to its parent object
(2) When you specify a parent object for a Qt object
① the parent object will include a pointer to the object in the child object linked list
② The object will hold a pointer to its parent object
(3) When the QT object is destroyed
① remove itself from the parent object's children list
② destroys all objects in its children list
When using QT development, we should not only pay attention to the problem of memory leak , but also keep an eye on whether the object can be destroyed multiple times .
Parent-child relationships between "programming experiments" objects
#include <QApplication>#include<QString>#include<QDebug>//Father-children Testvoidfctest () {Qobject* p =NewQobject (); //Only between Qobject and descendants can be a parent-child relationship Qobject* C1 =NewQobject (); Qobject* C2 =NewQobject (); C1-setParent (P); C2-setParent (P); //1. Print the addresses of the C1 and C2 two objects firstQdebug () <<"C1:"<< C1;//Print the address of the C1Qdebug () <<"C2:"<< C2;//Print the address of the C2//2. Output: Via P, output the address of its two children (expected results are C1 and C2) Constqobjectlist& list = p->Children (); for(inti =0; I < list.length (); i++) {qdebug ()<<List[i]; } qdebug ()<<"P:"<< p;//address of output PQdebug ()<<"c1->parent:"<< c1->parent (); Qdebug ()<<"c2->parent:"<< c2->parent ();}intMainintargcChar*argv[]) {Qapplication A (argc, argv); Fctest (); returna.exec ();}
(4) Using parent-child relationships between Qt objects can form an object tree, Delete a node in a tree will cause the corresponding sub-tree destroyed .
Deletion of the "Programming Experiment" object
#include <QApplication>#include<QString>#include<QDebug>//qt objects are inherited from Qobject.classMobj: Publicqobject{QString m_name; Public: Mobj (Constqstring&name) {M_name=name; Qdebug ()<<"Constructor:"<< This<<M_name; } ~mobj () {qdebug () <<"destructor:"<< This<<m_name;}};//Delete an object tree nodevoiddeltest () {mobj* Obj1 =NewMobj ("obj1"); Mobj* Obj2 =NewMobj ("Obj2"); Mobj* Obj3 =NewMobj ("obj3"); Mobj* Obj4 =NewMobj ("Obj4"); Obj2-setParent (OBJ1); Obj3-setParent (OBJ1); Obj4-setParent (OBJ3); DeleteObj3;//deleting obj3 will delete the child and remove it from the parent list! Constqobjectlist& list = obj1->Children (); Qdebug ()<<"Obj2:"<<Obj2; for(inti =0; I < list.length (); i++) {qdebug ()<< List[i];//Obj1 's children are left with OBJ2. }}intMainintargcChar*argv[]) {Qapplication A (argc, argv); Deltest (); returna.exec ();}
2. Summary
(1) Parent-child relationship can exist between Qt objects
(2) The Qt object tree can be obtained through parent-child relationship
(3) The parent- child relationship is lifted when the Qt object is destroyed
(4) When the Qt object is destroyed, all child objects are destroyed at the same time
16th lesson the parent-child relationship between Qt objects