qobjectcast
qobject_cast is a type-safe downcast utility in the Qt framework that uses the Qt meta-object system to cast pointers among QObject-derived types. It returns a pointer to the requested type if the object's runtime type matches the target type or a subclass, and returns nullptr otherwise. The cast relies on Qt’s runtime type information rather than C++ RTTI alone.
qobject_cast is intended for use with QObject hierarchies and with types that participate in the Qt meta-object
Compared with dynamic_cast, qobject_cast uses Qt’s meta-object data rather than C++ RTTI. This makes it suitable
QLabel *label = qobject_cast<QLabel*>(obj);
}
- Both source and target types must derive from QObject and participate in the meta-object system (must
- It cannot be used for non-QObject types or for arbitrary C++ downcasts.
- It is most useful when working with QObject hierarchies in Qt-based codebases.