Qt Toolkit - Meta Object System

| Classes - Annotated - Tree - Functions - Home - Structure

# Meta Object System

The Meta Object System in Qt is responsible for the signal/slot mechanism for communication between objects, runtime type information and the dynamic property system.

It is based on three things:

- the QObject class,
- the "Q_OBJECT" macro inside the private section of the class declaration and
- the Meta Object Compiler (moc).

The moc reads a C++ source file. If it finds one or more class declarations that contain the "Q_OBJECT" macro, it produces another C++ source file which contains the meta object code for this class. This generated source file is either #included into the class' source file or compiled and linked with the class implementation.

Apart from the possibility to use signals and slots for communication between objects - the main reason for introducing the system - the meta object code implements certain other features in QObject:

- a function className() that returns the class name as string during runtime, without requiring native runtime type information (RTTI) support through the C++ compiler.
- a function inherits() that returns whether an object is instance of a class that inherits a specified class within the QObject inheritance tree.
- a function tr() for scoped string translation as used for internationalization.
- two functions setProperty() and property() to dynamically set and get object properties by name.
- a function metaObject() that returns the associated meta object for the class.

While it is possible to use QObject as a base class without Q_OBJECT macro and without meta object code, neither signals and slots nor the other features described here will be available. From the meta object system's point of view, a QObject subclass without meta code is equivalent to its closest ancestor with meta object code. That means for example that className() will not return the actual name of your class, but the class name of this ancestor. We strongly recommend to use the macro in all subclasses of QObject regardless whether they actually use signals, slots and properties or not.