Home > Articles > Programming > C/C++

The QObject Class in Qt4

  • Print
  • + Share This
An important class to become familiar with is the one from which all Qt Widgets are derived: QObject. In this chapter, the authors discuss the QObject class in Qt4.
This chapter is from the book

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


QObject's Child Managment



Composite Pattern: Parents and Children



QApplication and the Event Loop



Q_OBJECT and moc: A Checklist



Values and Objects



tr() and Internationalization


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 {
    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.

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


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


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.

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.

9.1 QObject's Child Managment

Example 9.1 shows a QObject derived class.

Example 9.1. src/qobject/person.h

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

    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 <QTextStream>
static QTextStream cout(stdout, QIODevice::WriteOnly);

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

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

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 <QTextStream>
#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 << "\nDisplay the list using QObject::dumpObjectTree()"
             << endl;
    cout << "\nProgram 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 parent—memory 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
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
  • + Share This
  • 🔖 Save To Your Account