Possible garbage collection problem with QGraphicsItem



  • Hi,

    I noticed a situation in PySide where try to remove a QGraphicsItem from a parentItem/scene and it doesn't get remove from the scene when i call setParentItem(None). When I remove another (added by myself) reference to the QGraphicsItem it does work. Some code:
    @
    class Item(QGraphicsItem):

    def __init__(self, parent):
        super(Item, self).__init__(parent)
        self.select_target = self
        ...
    

    item = Item(anotheritem)
    item.setParentItem(None) #does not remove it from the scene, still gets hover events, etc.
    @

    however when I add:

    @
    def delete(self):
    self.select_target = None
    self.setParentItem(None)
    @

    and call that, the item is removed. I would expect both to have the same result, because of two reasons:

    • i thought items where responsible for painting their children and setParentItem(None) removes the item as a child
    • the self.select_target = self is a circular reference that should be detected by the garbage collector

    UPDATE:
    found in docs: using setParentItem(0) .. 'None' in PySide .. remove item from parent item AND MOVES IT TO THE SCENE. Using

    @
    def delete(self):
    self.setParentItem(None)
    self.scene().removeItem(self)
    @

    seems to work for me (now i think of it, might give a problem if parentItem or scene was never set, leave that as exercise). Still strange that self.select_target = None worked as well ....


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.