Why does Qt reimplement RTTI in qobject_cast, event type, etc?

992 Views Asked by At

Why does Qt bother reimplementing what amounts to a custom RTTI system and a their own dynamic_cast in the QObject hierarchy, in QEvent, etc?

1

There are 1 best solutions below

0
On

First of all, only a few class hierarchies in Qt actually need RTTI. When you generate embedded code you can save a whole bunch of code space by not emitting RTTI information. When building Qt and suitably written projects that use it, you can turn off RTTI in the compiler. You can't use dynamic_cast anymore, thus QObject hierarchy has its own qobject_cast, and QEvent uses explicit integer type tags.

A custom RTTI system for QObject hierarchy also allows dynamic creation of types from their metadata, as well of the metadata of new types that the compiler was unaware of. That's why QML can work, for example. In Qt 5, this functionality is offered by the private QMetaObjectBuilder. The legacy way of creating dynamic signals and slots wasn't compatible with standard QObject::connect.

Historically, with some compilers dynamic_cast failed across shared library boundaries.