Compound QGraphicsItem object sometimes remains as a "ghost" after deletion, occasionally will crash Python
-
Hi,
This is my first Qt/PySide project, so please forgive the myriad newbie errors!I am building a model-based graph editor (nodes & edges) - think a GUI for
networkx
.
The program maintains an 'abstract' mathematical model of the graph, aQListWidget
of the elements in the graph, andQGraphicsScene
of the nodes and edges graphically. Nodes areQGraphicsItem
s with text and an ellipse as children. Edges are alsoQGraphicsItem
s, with text, a line and an arrow as children. Adding and moving nodes and edges works fine. Edge end-points are moved as their connected node moves. Edges can successfully be moved from one node to another.
When an edge is deleted, around 50% of the time, it works perfectly. The other ~50%, there is a ghost object left in theScene
. It is deleted from the abstract model, and from theQListWidget
.
If I print out all thegraphicsView.items()
, the 'deleted' edge is not shown in the list, but it is present in the drawn view. If you move a node so that the view has to resize, then the ghost often (but not always) goes away. I have triedscene.update()
andscene.invalidate()
to tidy things up, but it does not help. I have tried a delayed forced repaint, also without results
QTimer.singleShot(500, lambda: self.ui.graphicsView.viewport().repaint())
If I force a
gc.collect
the entire Python shell crashes without a trace, literally and figuratively! If doing an explicit
self.Scene.render(painter, target=QRectF(), source=boundingRect)
it will also crash Python.It seems that PySide6 has issues with object references? Threads here and here.
Putting a strong reference on the child object, to 'hint' to the Python garbage collection as to when the object is still in use was helpful, but feels cludgy (even by newbie standards!)
self.textItem.my_parent_item = self
This is way beyond my abilities with Python garbage collection!
I would love to be able to recreate a "short" code fragment for this, but I'm not sure where the issue is. The
QGraphicsScene
acts as the 'controller', sending updates to the List and Model. The node and edge objects have around 5 and 10 defined methods each (boudingRect, shape, paint, etc) My use of signals and slots is very limited - this is mostly calls at this point.
The code, which is still the core the beginnings of the project, can be found on Github, here . I genuinely do not know which parts to extract to create the minimum code to reproduce the bug - it is somewhere in, or within, the interplay ofGraphicsScene
andGraphicsObject
s.I am pretty sure I am not constructing the compound
GraphicsObject
s correctly, as I have some consistently odd selection and clipboard painting issues, and this may well be related.I have spent many hours chasing this, and would love any guidance anyone might have.
Thanks in advance,
Grant