A large number of pointers in the QGraphicsView structure and performance

Since most of the convenient QGraphicsScene and QGraphicsItem functions (e.g. items (), collidingItems (), childItems (), etc.) return a QList, you are forced to do a lot of qgraphicsitem_cast or static_cast and QGraphicsItem :: Type () checks to get the actual items when you have many different items in the scene. I thought many subclasses were not the desired coding style, but I think that in this case there is 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 can be replaced with static_cast, since the correct type has already been checked. When you do a lot of them all the time (a very dynamic scene), will a lot of castings affect performance beyond the usual if-else evaluation?

+3
1

; , . type(). , item->type() . , , , , .

[] , , . . if (dynamic_cast<MyGraphicsInterMediateType*>(item)) {/* check those types */} else {/* other types */}

0

Source: https://habr.com/ru/post/1749348/


All Articles