toGraphicsObject
returns null pointer because QGraphicsRectItem
is not a QGraphicsObject
. You cannot use QGraphicsRectItem
to perform an animation. I can suggest two workarounds:
- Create your own class derived from
QObject
andQGraphicsRectItem
, create "pos" property and implement its getter and setter. See an example here. - Create your own class derived from
QGraphicsObject
. Implement itsboundingRect
andpaint
pure virtual methods to make it paint a rectangle.