[ fromfile: reflection.xml id: metaobjects ]
A meta object is an object that describes the structure of another object.[86]
A class that has a MetaObject supports reflection.
This is a feature found in many object-oriented languages.
It does not exist in C++, but Qt's MetaObject compiler, moc
, generates the code to
support this for specially equipped QObject
s.
As long as certain conditions apply,[87] each class derived from QObject has a QMetaObject generated for it by
moc
, similar to those we have for Customer and
Address, shown
in Figure 12.1.
QObject has a member function that
returns a pointer to the object's QMetaObject.
The prototype of that function is
QMetaObject* QObject::metaObject () const [virtual]
You can use these methods of QMetaObject to get information about a QObject:
className()
, which returns the class name as a const char*
superClass()
, which returns a pointer to the QMetaObject of the base
class if there is one (or 0
if there is not)
methodCount()
, which returns the number of member functions of the class
Several other useful functions that we discuss in this chapter
The signal and slot mechanism also relies on the QMetaObject.
Figure 12.1 shows the
inheritance relationships between the Qt base classes, derived QObject
s,
and the moc
-generated meta classes for them.
By using the QMetaObject and QMetaProperty, you can write code that is generic enough to handle all self-describing classes.
Generated: 2012-03-02 | © 2012 Alan Ezust and Paul Ezust. |