[prev in list] [next in list] [prev in thread] [next in thread]
List: pykde
Subject: [PyKDE] PyQt4 problems with underlying C/C++ objects [weird]
From: "Krystian Samp" <samp.krystian () gmail ! com>
Date: 2006-11-19 22:18:32
Message-ID: cfa896710611191418m32121c0r4e82d84a4ac258bc () mail ! gmail ! com
[Download RAW message or body]
[Attachment #2 (multipart/alternative)]
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
[Attachment #5 (text/html)]
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>
_______________________________________________
PyKDE mailing list PyKDE@mats.imk.fraunhofer.de
http://mats.imk.fraunhofer.de/mailman/listinfo/pykde
[prev in list] [next in list] [prev in thread] [next in thread]
Configure |
About |
News |
Add a list |
Sponsored by KoreLogic