Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

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)?



  • @clostridium_difficile

    1. 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.
    2. 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.
    3. You can always change an item's parent or scene later on
    4. Never use "setParent" on GraphicsItems that also happen to be QObjects! Always stick to setParentItem!


  • @clostridium_difficile

    1. 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.
    2. 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.
    3. You can always change an item's parent or scene later on
    4. Never use "setParent" on GraphicsItems that also happen to be QObjects! Always stick to setParentItem!

Log in to reply