16th lesson the parent-child relationship between Qt objects

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.