Hi,<br>thanks for the answer. When I commented the line which adds MyItem2 to the scene the effect was the same - the underlying C/C++ object was destructed. It is weird but I found some interesting notes on this problem:
<a href="http://www.nanoengineer-1.net/mediawiki/index.php?title=Qt_4_mysteries#One_mystery_solved">http://www.nanoengineer-1.net/mediawiki/index.php?title=Qt_4_mysteries#One_mystery_solved</a><br><br>It seems that when you create an item the python object holds a reference to it. However, if you assign that item to a hierarchy of items (for instance using setParentItem()) the python object loses (or you can say hands over) the reference. This reference still exists in the C/C++ level in the hierarchy but only there. So If you stop referencing to the parent of the item then the item itself will be destroyed as well. This is weird since you still have a python object representing the item but as I said it handed over the reference to the C/C++ layer. This is my observation and I think it's not a good behavior of PyQt since you actually have to think about C/C++ and the principle I've described.
<br><br>so for the above code snippet it is sufficient to detach children items from the parent before deleting the parent.<br><br>any comments? your observations?<br><br>best regards,<br>Krystian<br><br><div><span class="gmail_quote">
2006/11/21, Phil Thompson <<a href="mailto:phil@riverbankcomputing.co.uk">phil@riverbankcomputing.co.uk</a>>:</span><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
On Sunday 19 November 2006 10:18 pm, Krystian Samp wrote:<br>> Hi all,<br>><br>> I've got the following error:<br>><br>> Traceback (most recent call last):<br>> File "test2.py", line 47, in mousePressEvent
<br>> item = MyItem2(self.options)<br>> File "test2.py", line 10, in __init__<br>> i.setParentItem(self)<br>> RuntimeError: underlying C/C++ object has been deleted<br>><br>> for this code (I've tried to make it as short as possible):
<br>><br>> import sys<br>> from PyQt4 import QtGui, QtCore<br>><br>> class MyItem2(QtGui.QGraphicsItem):<br>> def __init__(self, items):<br>> QtGui.QGraphicsItem.__init__(self)<br>><br>
> self.items = items<br>> for i in self.items:<br>> i.setParentItem(self)<br>><br>> def paint(self, painter, option, widget):<br>> painter.drawEllipse(0, 0, 10, 10)
<br>><br>> def boundingRect(self):<br>> return QtCore.QRectF(0, 0, 20, 20)<br>><br>><br>> class MyItem1(QtGui.QGraphicsItem):<br>> def __init__(self):<br>> QtGui.QGraphicsItem.__init_
_(self)<br>><br>> def paint(self, painter, option, widget):<br>> painter.drawEllipse(0, 0, 20, 20)<br>><br>> def boundingRect(self):<br>> return QtCore.QRectF(0, 0, 20, 20)<br>>
<br>><br>> class MyItem(QtGui.QGraphicsItem):<br>> def __init__(self, options):<br>> QtGui.QGraphicsItem.__init__(self)<br>><br>> self.options = options<br>> self.activated
= False<br>> self.items = []<br>><br>> def paint(self, painter, option, widget):<br>> pass<br>><br>> def boundingRect(self):<br>> return QtCore.QRectF(-100, -100, 200, 200)
<br>><br>> def mousePressEvent(self, event):<br>> self.activated = not self.activated<br>> if self.activated:<br>> item = MyItem2(self.options)<br>> self.scene
().addItem(item)<br>> self.items.append(item)<br>> item.setParentItem(self)<br>> if not self.activated:<br>> for i in self.items:<br>> i.setParentItem
(None)<br>> self.scene().removeItem(i)<br>> self.items = []<br>><br>> class MyView(QtGui.QGraphicsView):<br>> def __init__(self):<br>> QtGui.QGraphicsView.__init__(self)
<br>><br>> self.scene = QtGui.QGraphicsScene()<br>><br>> self.items = [MyItem1(), MyItem1(), MyItem1()]<br>> self.item = MyItem(self.items)<br>><br>> self.scene.addItem
(self.item)<br>> self.setScene(self.scene)<br>><br>> # main program<br>> app = QtGui.QApplication(sys.argv)<br>> view = MyView()<br>> view.show()<br>><br>> sys.exit(app.exec_())<br>><br>
> After running it, click several times on the 'canvas' to see the error.<br>> It is strange. The error concerns objects which I hold in MyItem.objects. I<br>> do not understand why the underlying objects are lost. Can anyone explain
<br>> me that?<br>> Moreover if I comment the line 'self.items = []' then there is no such<br>> error. Is it possible that this assignment destroys C/C++ objects under the<br>> curtain? Hard to believe.<br><br>
You are adding MyItem2 to the scene and setting its parent to MyItem. My<br>understanding is that you do one or the other. If I'm wrong then there is<br>probably a bug in the ownership transfer flags.<br><br>Phil<br></blockquote>
</div><br>