Differences between qgraphicsitem_cast and dynamic_cast
Unsolved
General and Desktop
-
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?
-
qgraphicsitem_cast
basically doesif (item->type() == T::Type) return static_cast<T>(item)
so potentially a lot faster thandynamic_cast
, which needs to do runtime RTTI traversal.If you do that check often it's definitely worth it.