[PyKDE] PyQt4 problems with underlying C/C++ objects [weird]

Phil Thompson phil at riverbankcomputing.co.uk
Tue Nov 21 08:52:25 GMT 2006


On Sunday 19 November 2006 10:18 pm, Krystian Samp wrote:
> 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.

You are adding MyItem2 to the scene and setting its parent to MyItem. My 
understanding is that you do one or the other. If I'm wrong then there is 
probably a bug in the ownership transfer flags.

Phil




More information about the PyQt mailing list