Skip to content

Qt Jambi – QGraphicsScene itemAt with custom QGraphicsItem

This is for QtJambi

Ran into a funny problem with the itemAt() function of QGraphicsScene. I had a custom QGraphicsItem which is a circle with text in it (a node for a graph basically). The boundingRect, sceneBoundingRect, contains and all the other positional methods checks out correct with the custom item, but the itemAt function only works if I click on the bottom half of the shape.

Can’t really override the itemAt() method either since its final. But to write a custom itemAt, try the following:

private QGraphicsItemInterface itemAt(QGraphicsSceneMouseEvent e) {
for (QGraphicsItemInterface item : this.items())
if (item.sceneBoundingRect.contains(e.scenePos()))
return item;

return null;

Posted in Java, Qt. Tagged with , , , , , .