Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 483
Marty Avatar asked Nov 20 '25 03:11

Marty


1 Answers

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

like image 135
Chris Avatar answered Nov 21 '25 18:11

Chris



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!