Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct
Differences between qgraphicsitem_cast and dynamic_cast
michalmonday last edited by
Is there any reason for using qgraphicsitem_cast instead of dynamic_cast for recognizing sublass of QGraphicsItem?
qgraphicsitem_cast requires implementing "type()" method, and dynamic_cast doesn't require that (so dynamic_cast is simpler to use from my point of view), but is there any drawbacks to using dynamic_cast?
That's how I'm recognizing subclasses currently:
for (QGraphicsItem *item : childItems()) // (childItems of some parent GraphicsItem) if (SubclassGraphicsItem *sub_item = dynamic_cast<SubclassGraphicsItem *>(item)) //use sub_item
Is there some benefits to using qgraphicsitem_cast that will make it worth to implement "type()" method for each of the QGraphicsItem subclasses?
if (item->type() == T::Type) return static_cast<T>(item)so potentially a lot faster than
dynamic_cast, which needs to do runtime RTTI traversal.
If you do that check often it's definitely worth it.