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 do not understand why the underlying objects are lost. Can anyone explain me that?<br>Moreover if I comment the line 'self.items = []' then there is no such error. Is it possible that this assignment destroys C/C++ objects under the curtain? Hard to believe.
<br><br>best regards,<br>Krystian<br>