QGraphicItem, when you move a parent, shouldn't the children move with it?

  • I thought that if you move a parent QGraphicsItem, then all of it's children will also move? I have a class called Hexagon that inherits from QGraphicsPolygonItem. In it's constructor, I build a QGraphicsRectItem and set the parent of the rect item to be "this" (the Hexagon).

    Now later in my code, when I move the Hexagon, the rect item does NOT move.

    I have been working on this for HOURS and I cannot figure out why. I thought when you moved a parent, the children also moved, so why is the child NOT moving in this case?!?!?!?!?!? (sorry, a bit frusterated)

  • Never mind, JUST figured out the problem. In the constructor of the parent, i created the child and added the child to the scene when the parent was already added to the scene.

    When I removed the line that added the child to the scene, everything works perfectly.

    I hope this is helpful to others who may ever come across this frusterating bug.

  • That is not a bug !

    Adding an Item to the scene will cause that the scene take ownership of your item. Your item is added as a top level item.
    Looking at the documentation will explain this to you :

    On the other side when you create an QGraphicsItem with a parent item it is automatically added to the scene, because all QGraphicsItems paint their children.
    When you pass no parent you must use addItem which is also explained in the documentation.

Log in to reply