Table of Contents
[ 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 ... };
QObject does not have a public
copy constructor or copy assignment operator.
explicit Constructors | |
---|---|
Single-argument constructors of QObject (and derived classes) should be designated |
The child list establishes a bidirectional association between QObjects:
Each parent object knows the address of each of its child objects.
Each child object knows the address of its parent object.
Setting the parent of a QObject implicitly adds its address to the child list of the parent; i.e.,
objA->setParent(objB);
adds the objA
pointer to the child list of objB
.
If you 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
.
Such an action is called reparenting.
As with QObjects, we refer to any object of a class publicly derived from QWidget as a QWidget (or sometimes, simply, widget).
In Figure 8.1, the dialog widget has several children, including: A label widget, a line edit widget, and two pushbutton widgets.
The need for the child management requirement is also visible in Figure 8.1.
That is why each QObject is responsible for destroying all its children when its destructor is called.
Generated: 2012-03-02 | © 2012 Alan Ezust and Paul Ezust. |