Chapter 8.  QObject, QApplication, Signals, and Slots

Table of Contents

8.1. Values and Objects
8.2. Composite Pattern: Parents and Children
8.2.1. Finding Children
8.2.2. QObject's Child Managment
8.2.2.1. Exercises: QObject's Child Managment
8.3. QApplication and the Event Loop
8.4. Q_OBJECT and moc: A checklist
8.5. Signals and Slots
8.5.1. Points of Departure
8.6. QObject Lifecycle
8.7. QTestLib
8.8. Exercises: QObject, QApplication, Signals, and Slots
8.9. Review Questions

[ fromfile: qobject.xml id: qobject ]

Abstract

QObject is the base class for many of the important classes in the Qt library, such as QEvent, QApplication, QLayout, and QWidget. A QObject can have a parent and children, providing another implementation of the Composite pattern. It can use signals and slots, an implementation of the Observer pattern, to communicate with other QObjects. QObjects make it possible to do event-based programming, which employs QApplication and Qt's event loop.

  class QObject {
    public:
      explicit QObject(QObject* parent=0);
      QObject * parent () const;
      QString objectName() const;
      void setParent ( QObject * parent );
      const QObjectList & children () const;
      // ... more ...
  };
 
[Note] explicit Constructors

Single-argument constructors of QObject (and derived classes) should be designated explicit to avoid accidental conversions from taking place.[36] QObject is not intended to be a holder of a temporary value, and it should not be possible to create one implicitly from a pointer or a simple value.

Figure 8.1.  Parent-Child Object Versus Base-Derived Class

Parent-Child Object Versus Base-Derived Class



[36] Section 2.12 discusses the keyword explicit.