qtpyqtqgraphicsscene

QGraphicsScene.items() only returning items whose pos() exactly matches a test point, not items which contain the test point


A QGraphicsScene contains a number of MyItem items.

Where scene_pos is a QPointF, I try

items = scene.items(scene_pos, selection_mode)

and I want to get every MyItem whose boundingRect() contains scene_pos

These are the behaviours I observe:

selection_mode behaviour
Qt.ContainsItemShape items is empty
Qt.IntersectsItemShape items contains an item if item.pos()==scene_pos
Qt.ContainsItemBoundingRect items is empty
Qt.IntersectsItemBoundingRect items contains an item if item.pos()==scene_pos

I also tried replacing scene_pos with a small QRectF around this pos -- same behaviour.

If I'm reading the documentation for ItemSelectionMode correctly, I want IntersectsItemBoundingRect

The output list contains both items whose bounding rectangle is fully contained inside the selection area, and items that intersect with the area's outline. This method is commonly used for determining areas that need redrawing.

If an ellipse with radius, say 100, has local origin (0,0) and its parent item pos() is, say, (1000,1000) and scene_pos is (1001,1001) (or using something like QRectF(999,999,2,2)) then scene_pos is clearly within the ellipse, and hence intersects it, right? In this example it would only work if scene_pos is (1000,1000).

Why isn't this working as expected?


Solution

  • The issue is that the boundingRect is not updated when children are updated, and the application must manage this explicitly.