Home | All Classes | Main Classes | Annotated | Grouped Classes | Functions |

# Meta-Object System

Qt's Meta-Object System provides the signals and slots mechanism for inter-object communication, runtime type information, and the dynamic property system.

The Meta-Object System is based on three things:

1. the QObject class
2. the `Q_OBJECT` macro inside the private section of the class declaration
3. the Meta-Object Compiler (`moc`)

The `moc` tool 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 the classes that contain the `Q_OBJECT` macro. This generated source file is either `#include`'d into the class's source file or compiled and linked with the class's implementation.

In addition to providing the signals and slots mechanism for communication between objects (the main reason for introducing the system), the meta-object code provides additional features in QObject:

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

It is also possible to perform dynamic casts using qt_cast<T>() on QObject classes. The qt_cast<T>() function behaves similarly to the standard C++ `dynamic_cast<T>()`, with the advantages that it doesn't require RTTI support and it works across dynamic library boundaries. It attempts to cast its argument to the pointer type specified in angle-brackets, returning a casted pointer if the object is of the correct type (determined at run-time) and 0 otherwise.

For example, let's assume `MyWidget` inherits from QWidget and is declared with the `Q_OBJECT` macro:

            QObject *obj = new MyWidget;

The `obj` variable, of type `QObject *`, actually refers to a `MyWidget` object.

            QWidget *widget = qt_cast<QWidget *>(obj);

The cast from QObject to QWidget is successful, because the object is actually a `MyWidget`, which is a widget.

            MyWidget *myWidget = qt_cast<MyWidget *>(obj);

Likewise, the cast to `MyWidget` is successful. qt_cast<T>() makes no distinction between built-in Qt types and custom types.

            QLabel *label = qt_cast<QLabel *>(obj);
            // label is 0

The cast to QLabel, on the other hand, fails. The pointer is then set to 0. This makes it possible to handle objects of different types differently at run-time, based on the type:

            if (QLabel *label = qt_cast<QLabel *>(obj)) {
                label->setText(tr("Ping"));
            } else if (QPushButton *button = qt_cast<QPushButton *>(obj)) {
                button->setText(tr("Pong!"));
            }

While it is possible to use QObject as a base class without the `Q_OBJECT` macro and without meta-object code, neither signals and slots nor the other features described here will be available if the `Q_OBJECT` macro is not used. 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. This means for example, that className() will not return the actual name of your class, but the class name of this ancestor.

Therefore, we strongly recommend that all subclasses of QObject use the `Q_OBJECT` macro regardless of whether or not they actually use signals, slots, and properties.