.NODE

QObject

An important class to become familiar with is the one from which all Qt Widgets are derived: QObject.

9.1

QObject's Child Managment

194

9.2

Composite Pattern: Parents and Children

196

9.3

QApplication and the Event Loop

200

9.4

Q_OBJECT and moc: A Checklist

209

9.5

Values and Objects

210

9.6

tr() and Internationalization

211

We will refer to any object of a class derived from QObject as a QObject. Here is an abbreviated look at its definition.

class QObject {
 public:
 QObject(QObject* parent=0);
 QObject * parent () const;
 QString objectName() const;
 void setParent ( QObject * parent );
 const ObjectList & children () const;
 // ... more ...
};

The first interesting thing that we observe is that QObject's copy constructor is not public. QObjects are not meant to be copied. In general, QObjects are intended to represent unique objects with identity; that is, they correspond to real-world things that also have some sort of persistent identity. One immediate consequence of not having access to its copy constructor is that a QObject can never be passed by value to any function. Copying a QObject's data members into another QObject is still possible, but the two objects are still considered unique.

One immediate consequence of not having access to its copy constructor is that QObjects can never be passed by value to any function.

Each QObject can have (at most) one parent object and an arbitrarily large container of QObject* children. Each QObject stores pointers to its children in a QObjectList.[1] The list itself is created in a lazy-fashion to minimize the overhead for objects which do not use it. Since each child is a QObject and can have an arbitrarily large collection of children, it is easy to see why copying QObjects is not permitted.

[1] QObjectList is a typedef (i.e., an alias) for QList

The notion of children can help to clarify the notion of identity and the no-copy policy for QObjects. If you represent individual humans as QObjects, the idea of a unique identity for each QObject is clear. Also clear is the idea of children. The rule that allows each QObject to have at most one parent can be seen as a way to simplify the implementation of this class. Finally, the no-copy policy stands out as a clear necessity. Even if it were possible to "clone" a person (i.e., copy its data members to another QObject), the question of what to do with the children of that person makes it clear that the clone would be a separate and distinct object with a different identity.

Each QObject parent manages its children. This means that the QObject destructor automatically destroys all of its child objects.

The child list establishes a bidirectional, one-to-many association between objects. Setting the parent of one object implicitly adds its address to the child list of the other, for example

objA->setParent(objB);

adds the objA pointer to the child list of objB. If we subsequently have

objA->setParent(objC);

then the objA pointer is removed from the child list of objB and added to the child list of objC . We call such an action reparenting.

Parent Objects versus Base Classes

Parent objects should not be confused with base classes. The parent-child relationship is meant to describe containment, or management, of objects at runtime. The base-derived relationship is a static relationship between classes determined at compile-time.

It is possible that a parent can also be an instance of a base class of some of its child objects. These two kinds of relationships are distinct and must not be confused, especially considering that many of our classes will be derived directly or indirectly from QObject.

It is already possible to understand some of the reasons for not permitting QObjects to be copied. For example, should the copy have the same parent as the original? Should the copy have (in some sense) the children of the original? A shallow copy of the child list would not work because then each of the children would have two parents. Furthermore, if the copy gets destroyed (e.g., if the copy was a value parameter in a function call), each child needs to be destroyed too. Even with resource sharing methods, this approach would introduce some serious difficulties. A deep copy of the child list could be a costly operation if the number of children were large and the objects pointed to were large. Since each child could also have arbitrarily many children, this questionable approach would also generate serious difficulties.


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

show all menu





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