QGraphicsScene::itemAt() - how to recognize custom classes

qgraphicsscene, qt

Solution

GraphicsItem::type() is intended to be used to solve this problem.

So you would do something like this for example:

enum ItemType { TypePNItem = QGraphicsItem::UserType + 1,
                TypePNEdge = QGraphicsItem::UserType + 2 }

class PNItem : public QObject, public QGraphicsItem {

    public:
        int type() { return TypePNItem; }
    ...

};

Which would then allow you to do this:

QGraphicsItem *item = scene->itemAt( x, y );
switch( item->type() )
{
    case PNItem:
         ...
         break;
}

doing this also enables the usage of qgraphicsitem_cast

See also: QGraphicsItem::UserType

Problem

i have a little problem I am programming Petri Net simulator... I have two different classes ``` class PNItem : public QObject, public QGraphicsItem ... ``` and ``` class PNEdge : public QGraphicsLineItem ``` when i call... ``` QGraphicsItem *QGraphicsScene::ItemAt(//cursor position) ``` , is it possible somehow to get to know, what item i have clicked on? resp. what item is given item by ItemAt?

Original source