The infamous "deleted graphics item reappears" bug is present in my app, how do I fix it?
-
That is the symptom pictured above. As you can see, those arrows will reappear, in particular when I'm dragging an object over them, and sometimes I believe when an arrow gets deleted but it's still attached to something.
Anyway, what is a design pattern that I can use for a DeleteUndoCmd? I.e. whatever the deletion does, it must be undoable which means re-attaching arrows to nodes if need be.
Anyway, I know about all that and coded my app robustly and even used a deleteLater() in there somewhere. However, as you can see items are not actually being removed from the scene (deleted).
The PyQt5 code can be found here: https://github.com/enjoysmath/abstract-spacecraft
Anyway, before I tackle this bug, I would like to know the proper / the only way to really delete something in a PyQt5 QGraphcisScene.
-
Nevermind! Modifying
DeleteItems
with a_connectivity
dictionary seems to have solved the problem.class DeleteItems(UndoCmd): Parent, Source, Dest = range(3) def __init__(self, items:list, canvas:LanguageCanvas): super().__init__() self._items = items self._canvas = canvas self._connectivity = {} def redo(self): for item in self._items: if isinstance(item, Arrow): self._connectivity[id(item)] = (item.parentItem(), item.source, item.destination) item.set_source(None) item.set_destination(None) elif isinstance(item, (Object, Text)): self._connectivity[id(item)] = item.parentItem() item.setParentItem(None) self._canvas.removeItem(item) def undo(self): for item in self._items: if isinstance(item, Arrow): parent, source, dest = self._connectivity[id(item)] item.set_source(source) item.set_destination(dest) elif isinstance(item, (Object, Text)): parent = self._connectivity[id(item)] item.setParentItem(parent) self._canvas.addItem(item)
I honestly thought this would be much tougher to fix.