[PyKDE] PyQt4 problems with underlying C/C++ objects [weird]
Krystian Samp
samp.krystian at gmail.com
Sun Nov 19 22:18:32 GMT 2006
Hi all,
I've got the following error:
Traceback (most recent call last):
File "test2.py", line 47, in mousePressEvent
item = MyItem2(self.options)
File "test2.py", line 10, in __init__
i.setParentItem(self)
RuntimeError: underlying C/C++ object has been deleted
for this code (I've tried to make it as short as possible):
import sys
from PyQt4 import QtGui, QtCore
class MyItem2(QtGui.QGraphicsItem):
def __init__(self, items):
QtGui.QGraphicsItem.__init__(self)
self.items = items
for i in self.items:
i.setParentItem(self)
def paint(self, painter, option, widget):
painter.drawEllipse(0, 0, 10, 10)
def boundingRect(self):
return QtCore.QRectF(0, 0, 20, 20)
class MyItem1(QtGui.QGraphicsItem):
def __init__(self):
QtGui.QGraphicsItem.__init__(self)
def paint(self, painter, option, widget):
painter.drawEllipse(0, 0, 20, 20)
def boundingRect(self):
return QtCore.QRectF(0, 0, 20, 20)
class MyItem(QtGui.QGraphicsItem):
def __init__(self, options):
QtGui.QGraphicsItem.__init__(self)
self.options = options
self.activated = False
self.items = []
def paint(self, painter, option, widget):
pass
def boundingRect(self):
return QtCore.QRectF(-100, -100, 200, 200)
def mousePressEvent(self, event):
self.activated = not self.activated
if self.activated:
item = MyItem2(self.options)
self.scene().addItem(item)
self.items.append(item)
item.setParentItem(self)
if not self.activated:
for i in self.items:
i.setParentItem(None)
self.scene().removeItem(i)
self.items = []
class MyView(QtGui.QGraphicsView):
def __init__(self):
QtGui.QGraphicsView.__init__(self)
self.scene = QtGui.QGraphicsScene()
self.items = [MyItem1(), MyItem1(), MyItem1()]
self.item = MyItem(self.items)
self.scene.addItem(self.item)
self.setScene(self.scene)
# main program
app = QtGui.QApplication(sys.argv)
view = MyView()
view.show()
sys.exit(app.exec_())
After running it, click several times on the 'canvas' to see the error.
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?
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.
best regards,
Krystian
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.riverbankcomputing.com/pipermail/pyqt/attachments/20061119/b5ab65dd/attachment.html
More information about the PyQt
mailing list