.NODE

QObjects Child Managment

QObject s Child Managment

Example 9.1 shows a QObject derived class.

Example 9.1. src/qobject/person.h

[ . . . . ]
class Person : public QObject {

 public:
 Person(QObject* parent, QString name);
 virtual ~Person();
};
[ . . . . ]

The complete implementation is shown in Example 9.2 to show that there is no explicit object deletion done in ~Person().

Example 9.2. src/qobject/person.cpp

#include "person.h"
#include 
static QTextStream cout(stdout, QIODevice::WriteOnly);

Person::Person(QObject* parent, QString name)
 : QObject(parent) {
 setObjectName(name);
 cout << QString("Constructing Person: %1").arg(name) << endl;
}

Person::~Person() {
 cout << QString("Destroying Person: %1").arg(objectName()) <<
 endl;
}

main(), shown in Example 9.3, creates some objects, adds them to other objects, and then exits. All heap objects were implicitly destroyed.

Example 9.3. src/qobject/main.cpp

#include 
#include "person.h"

static QTextStream cout(stdout, QIODevice::WriteOnly);
int main(int , char**) {
 cout << "First we create a bunch of objects." << endl;
 Person bunch(0, "A Stack Object"); <-- 1
 /* other objects are created on the heap */
 Person *mike = new Person(&bunch, "Mike");
 Person *carol = new Person(&bunch, "Carol");
 new Person(mike, "Greg"); <-- 2
 new Person(mike, "Peter");
 new Person(mike, "Bobby");
 new Person(carol, "Marcia");
 new Person(carol, "Jan");
 new Person(carol, "Cindy");
 new Person(0, "Alice"); <-- 3
 cout << "
Display the list using QObject::dumpObjectTree()"
 << endl;
 bunch.dumpObjectTree();
 cout << "
Program finished - destroy all objects." << endl;
 return 0;
}
 

(1)not a pointer

(2)We do not need to remember pointers to children, since we can reach them via object navigation.

(3)Alice has no parentmemory leak?

Here is the output of this program:

First we create a bunch of objects.
Constructing Person: A Stack Object
Constructing Person: Mike
Constructing Person: Carol
Constructing Person: Greg
Constructing Person: Peter
Constructing Person: Bobby
Constructing Person: Marcia
Constructing Person: Jan
Constructing Person: Cindy
Constructing Person: Alice

Display the list using QObject::dumpObjectTree()
QObject::A Stack Object
 QObject::Mike
 QObject::Greg
 QObject::Peter
 QObject::Bobby
 QObject::Carol
 QObject::Marcia
 QObject::Jan
 QObject::Cindy
Program finished - destroy all objects.
Destroying Person: A Stack Object
Destroying Person: Mike
Destroying Person: Greg
Destroying Person: Peter
Destroying Person: Bobby
Destroying Person: Carol
Destroying Person: Marcia
Destroying Person: Jan
Destroying Person: Cindy

Notice that Alice is not part of the dumpObjectTree() and does not get destroyed.

Exercise: QObject's Child Managment

Add the function

void showTree(QObject* theparent)

to main.cpp. The output of this function, after all objects have been created, should look like this:

Member: Mike - Parent: A Stack Object
Member: Greg - Parent: Mike
Member: Peter - Parent: Mike
Member: Bobby - Parent: Mike
Member: Carol - Parent: A Stack Object
Member: Marcia - Parent: Carol
Member: Jan - Parent: Carol
Member: Cindy - Parent: Carol


Composite Pattern Parents and Children

Part I: Introduction to C++ and Qt 4

C++ Introduction

Classes

Introduction to Qt

Lists

Functions

Inheritance and Polymorphism

Part II: Higher-Level Programming

Libraries

Introduction to Design Patterns

QObject

Generics and Containers

Qt GUI Widgets

Concurrency

Validation and Regular Expressions

Parsing XML

Meta Objects, Properties, and Reflective Programming

More Design Patterns

Models and Views

Qt SQL Classes

Part III: C++ Language Reference

Types and Expressions

Scope and Storage Class

Statements and Control Structures

Memory Access

Chapter Summary

Inheritance in Detail

Miscellaneous Topics

Part IV: Programming Assignments

MP3 Jukebox Assignments

Part V: Appendices

MP3 Jukebox Assignments

Bibliography

MP3 Jukebox Assignments





An Introduction to Design Patterns in C++ with Qt 4
An Introduction to Design Patterns in C++ with Qt 4
ISBN: 0131879057
EAN: 2147483647
Year: 2004
Pages: 268
Similar book on Amazon

Flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net