I see you have answered your own question. However I would like to explain why this works.
Every QGraphicsItem
has its own local coordinate system. So when you do
self.rect = QRectF(position.x(), position.y(), 15, 15)
you basically start from the (0, 0)
of the item's local coordinate system and go to the given x
and y
which you take from position
. This basically means that your rectangle will be drawn at position.x() + position.x()
and position.y() + position.y()
with the first position.x()
/position.y()
being the position of the QGraphicsItem
inside your scene and the second position.x()
/position.y()
being the position inside the local coordinate system of your item.
If you want to start from the origin of the QGraphicsItem
, you have to use
self.rect = QRectF(0, 0, 15, 15)
This ensures that you start from the origin of the local coordinate system.
This issue is particularly tricky due to the fact that by default objects are added to the (0, 0)
of a scene. So position.x() + position.x()
and position.y() + position.y()
in this case will actually not show the issue at hand since 0+0
is always equal to 0
. It is the moment you change the default position to something else when the problem will occur.
Here is a 3D figure that visualizes what I'm describing above (I was unable to find a 2D example but the principle is the same :P):
The world
here is the scene while the object
is the QGraphicsItem
residing in that scene
.