Lots of pointer casts in QGraphicsView framework and performance
Posted
by kleimola
on Stack Overflow
See other posts from Stack Overflow
or by kleimola
Published on 2010-06-10T06:36:03Z
Indexed on
2010/06/10
6:43 UTC
Read the original article
Hit count: 196
Since most of the convenience functions of QGraphicsScene and QGraphicsItem (such as items(), collidingItems(), childItems() etc.) return a QList you're forced to do lots of qgraphicsitem_cast or static_cast and QGraphicsItem::Type() checks to get hold of the actual items when you have lots of different type of items in the scene. I thought doing lots of subclass casts were not a desirable coding style, but I guess in this case there are no other viable way, or is there?
QList<QGraphicsItem *> itemsHit = someItem->collidingItems(Qt::IntersectsItemShape);
foreach (QGraphicsItem *item, itemsHit) {
if (item->type() == QGraphicsEllipseItem::type()) {
QGraphicsEllipseItem *ellipse = qgraphicsitem_cast<QGraphicsEllipseItem *>(item);
// do something
}
else if (item->type() == MyItemSubclass::type()) {
MyItemSubClass *myItem = qgraphicsitem_cast<MyItemSubClass *>(item);
// do something
}
// etc
}
The above qgraphicsitem_cast could be replaced by static_cast since correct type is already verified. When doing lots of these all the time (very dynamic scene), will the numerous casting affect performance beyond the normal if-else evaluation?
© Stack Overflow or respective owner