From pykde Sun Nov 19 22:18:32 2006 From: "Krystian Samp" Date: Sun, 19 Nov 2006 22:18:32 +0000 To: pykde Subject: [PyKDE] PyQt4 problems with underlying C/C++ objects [weird] Message-Id: X-MARC-Message: https://marc.info/?l=pykde&m=116397548905072 MIME-Version: 1 Content-Type: multipart/mixed; boundary="--===============1001128085==" --===============1001128085== Content-Type: multipart/alternative; boundary="----=_Part_31607_25073368.1163974712865" ------=_Part_31607_25073368.1163974712865 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Content-Disposition: inline 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 ------=_Part_31607_25073368.1163974712865 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit Content-Disposition: inline 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
------=_Part_31607_25073368.1163974712865-- --===============1001128085== Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Disposition: inline _______________________________________________ PyKDE mailing list PyKDE@mats.imk.fraunhofer.de http://mats.imk.fraunhofer.de/mailman/listinfo/pykde --===============1001128085==--