Solved Does every QGraphicsItem need to be QGraphicsScene's child?
-
I am developing Raster Graphics Editor. I am confused because I have read that QGraphicsScene's addItem() method adds QGraphicsItem, and all it's children, to scene, but in other forums people says that every item needs to be added to scene apart from other, parent QGraphicsItem. What is the correct approach? Other question: how to remove/detach child graphics item from it's parent? If QGraphicsItem was created by new operator, then set parent to other graphics item, who is responsible for freeing memory? Does QGraphicsItem's destructor invokes delete operator for every child (if at all)?
-
- You either add an item to a scene directly, OR use setParentItem to set it's parent. It will then automatically be added to the scene of that parent.
- As soon as an item has a scene or parent, that scene or parent is its owner. When the scene is destroyed, all items belonging to that scene are destroyed. When a parent item is destroyed, all it's children are destroyed.
- You can always change an item's parent or scene later on
- Never use "setParent" on GraphicsItems that also happen to be QObjects! Always stick to setParentItem!
-
- You either add an item to a scene directly, OR use setParentItem to set it's parent. It will then automatically be added to the scene of that parent.
- As soon as an item has a scene or parent, that scene or parent is its owner. When the scene is destroyed, all items belonging to that scene are destroyed. When a parent item is destroyed, all it's children are destroyed.
- You can always change an item's parent or scene later on
- Never use "setParent" on GraphicsItems that also happen to be QObjects! Always stick to setParentItem!