What are MetaObjects?

MetaObject is an object that describes the structure of other object (properties and methods). Around other functionalities, a metaobject can know the type, name and value of the variable of the associated object in execution times.

Thanks for the metaobject system of Qt, it is possible to use:

  1. Signals and Slots.
  2. RTTI– Run Time Type Information.
  3. Dynamic properties system.

To use the metaobject system of Qt in determined objects, a certain object shall inherite from QObject and be compiled by the MetaObject Compiler (moc).

For example, SpinBox inherites from QObject and automatically it is compiled by moc.

The previous code prints all the properties from the QSpinBox object in the following way:

Signals and Slots

The Signals and Slots system is used for the communication between QObjects. This mechanism is a milestone of Qt.

Signals and Slots is a alternative techinique to use rather than Callbacks (callbacks are based in function pointers). One diferent between the previous menctioned technices is that Signals and Slots validates the parameters type and the connected classes do not need know anything from the other one.

 

RTTI

RTTI is a system to determine the execution times of a object type. The operator qobject_cast is one of the RTTI tools of Qt.

qobject_cast permits the cast of pointers to general references to specific references in execution times. qobject_cast can work 5 to 10 times faster than dynamic_cast of C++ ( this of course, depending of the used compiler).

The following code determiens dynamically the type of the country that it is used.

 

 

Dynamic Properties

To make the meta object system detect properties of a object, it shall be declared with the macro Q_PROPERTY.

The dynamic properties can be identifed in execution times and be part of an object instance. The function QObject::setProperty() permits the introduction of dynamic properties.

Las propiedades dinámicas pueden ser registradas en tiempos de ejecución y ser parte de una instancia de objeto. La función QObject::setProperty() permite introducir propiedades dinámicas.

Leave a Reply

Your email address will not be published. Required fields are marked *