How to change parent of scaled QGraphicsItem while retaining scene bounding rect.
I am trying to change the parent of a QGraphicsItem without it appearing to move or change size on the scene. Both the item, old parent, and new have arbitrary scale values set. How can this be done?
I got a little further, this snippet works when the parent items don’t have a scale set. But when the parents do have a scale set the child item’s position jumps around a bit when changing parents.
So I’m not sure what to do next.
def setParent(parent): if parent == child.parentItem(): return if not child.parentItem() and parent: st = child.sceneTransform() # I assume even if the child has a parent. inverted, ok = parent.sceneTransform().inverted() lt = inverted * st elif child.parentItem() and not parent: lt = child.sceneTransform() elif child.parentItem() and parent: st = child.sceneTransform() inverted, ok = parent.sceneTransform().inverted() lt = inverted * st child.setTransform(lt) child.setScale(1.0) child.setRotation(0.0) child.setParentItem(parent)