While trying to implement a scene where item sizes do not change but distances between items get magnified I encountered this problem with the following code which draws a recta
In fact, the rectangle does not vanish. But it moves around "strangely".
self.itemB = QtGui.QGraphicsRectItem(30, 50, 20, 20)
This line may not be what you want. This creates an item and puts a rectangle/square starting from (30, 50)
in local coordinates. Then you add this to the scene. This gives you an item anchored at (0, 0)
, spans up to (50, 70)
but draws a rectangle only in the bottom right 20x20
.
When you set ItemIgnoresTransformations
, item can't do its regular transformations in case of a zoom. Scene zooms in, for item to ignore this transformation, it kind of "shrinks" itself. But it's still anchored at (0, 0)
and the rectangle is at the bottom-right, so the drawn rectangle moves toward the upper-left.
Solution is rather simple. Don't create your rectangle in local coordinates, i.e. your rectangle should start from (0, 0)
and you should position it explicitly. Which translates to this:
self.itemB = QtGui.QGraphicsRectItem(0, 0, 20, 20)
self.itemB.setPos(30, 50)