In MenuItem()
constructor you use mapToParent
. But your item doesn't have any parent item. So using mapToParent
is pointless, it's equivalent to mapToScene
in this case. And since your item's initial position is (0, 0) and no transformation has been applied, mapToScene
will return its argument's value without changes. So it's equivalent to setPos(200, 200)
. It seems strange to use the result of mapToParent
or mapToScene
in setPos
. I don't understand what you were trying to do.
QGraphicsSceneMouseEvent::pos
returns coordinates of the event in target item's coordinates. Since you're using it in QGraphicsScene::keyPressEvent
, the event has not been propagated to any item, and pos()
always returns (0, 0). The documentation isn't clear about it, but I've checked it.
If you didn't use setPos
, your item's position will be (0, 0) and itemAt(0, 0)
will find your item (regardless of the point the user have actually clicked). But if you did use setPos
, itemAt(0, 0)
returns 0 because there is no item at this point. If you replace event->pos()
with event->scenePos()
, it will work correct.
However, it's unusual to reimplement QGraphicsScene::keyPressEvent
to catch clicking on item. You should reimplement QGraphicsItem::mousePressEvent
instead. It will be called only if the item has been clicked, and you don't have to check event's coordinates to determine that.