[prev in list] [next in list] [prev in thread] [next in thread]
List: pykde
Subject: Re: [PyQt] Subclass of QGraphicsObject does not have the correct
From: Selim Tuvi <Selim.Tuvi () pdi ! dreamworks ! com>
Date: 2011-05-20 16:01:34
Message-ID: 4DD6905E.7050206 () pdi ! dreamworks ! com
[Download RAW message or body]
[Attachment #2 (multipart/alternative)]
Hi Phil, thanks for the explanation. The issue came up where I need to
do special processing based on the type of the item. I'll rely on my own
"itemtype" property instead.
-Selim
Phil Thompson wrote:
> On Thu, 19 May 2011 14:42:47 -0700, Selim Tuvi
> <Selim.Tuvi@pdi.dreamworks.com> wrote:
>
>> The code below outputs:
>>
>> [<__main__.Edge object at 0x2aaab0395830>, <PyQt4.QtGui.QGraphicsItem
>> object at 0x2aaab03958c0>]
>>
>> which lists the Node instance as having the class QGraphicsItem when it
>> should say <__main__.Node object at ...>.
>>
>> Tested on (Qt 4.7.2, PyQt 4.8.3) and (Qt 4.6.1, PyQt 4.7.2)
>>
>> Thanks
>> -Selim
>>
>> from PyQt4 import QtGui, QtCore
>>
>> class Node(QtGui.QGraphicsObject):
>> def __init__(self):
>> QtGui.QGraphicsObject.__init__(self)
>>
>> def paint(self, painter, option, widget):
>> pass
>>
>> def boundingRect(self):
>> return QtCore.QRectF()
>>
>> class Edge(QtGui.QGraphicsItem):
>> def __init__(self):
>> QtGui.QGraphicsItem.__init__(self)
>>
>> def paint(self, painter, option, widget):
>> pass
>>
>> def boundingRect(self):
>> return QtCore.QRectF()
>>
>> if __name__ == '__main__':
>> import sys
>> app = QtGui.QApplication(sys.argv)
>> print QtCore.QT_VERSION_STR, QtCore.PYQT_VERSION_STR
>> view = QtGui.QGraphicsView()
>> scene = QtGui.QGraphicsScene()
>> view.setScene(scene)
>> scene.addItem(Node())
>> scene.addItem(Edge())
>> print scene.items()
>> view.show()
>> sys.exit(app.exec_())
>>
>
> It's because QGraphicsObject inherits both QObject and QGraphicsItem.
> items() returns a list of QGraphicsItems which, for a QGraphicsObject, has
> a different C++ address than the original QGraphicsObject. PyQt doesn't
> recognise that the QGraphicsItem is a cast of the QGraphicsObject. I don't
> think there is anything I can (sensibly) do about this.
>
> Phil
>
[Attachment #5 (text/html)]
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta content="text/html;charset=UTF-8" http-equiv="Content-Type">
</head>
<body bgcolor="#ffffff" text="#000000">
Hi Phil, thanks for the explanation. The issue came up where I need to
do special processing based on the type of the item. I'll rely on my
own "itemtype" property instead.<br>
-Selim<br>
<br>
Phil Thompson wrote:
<blockquote cite="mid:e246ea0cb170d893c9223b12581a69df@localhost"
type="cite">
<pre wrap="">On Thu, 19 May 2011 14:42:47 -0700, Selim Tuvi
<a class="moz-txt-link-rfc2396E" \
href="mailto:Selim.Tuvi@pdi.dreamworks.com"><Selim.Tuvi@pdi.dreamworks.com></a> \
wrote: </pre>
<blockquote type="cite">
<pre wrap="">The code below outputs:
[<__main__.Edge object at 0x2aaab0395830>, <PyQt4.QtGui.QGraphicsItem
object at 0x2aaab03958c0>]
which lists the Node instance as having the class QGraphicsItem when it
should say <__main__.Node object at ...>.
Tested on (Qt 4.7.2, PyQt 4.8.3) and (Qt 4.6.1, PyQt 4.7.2)
Thanks
-Selim
from PyQt4 import QtGui, QtCore
class Node(QtGui.QGraphicsObject):
def __init__(self):
QtGui.QGraphicsObject.__init__(self)
def paint(self, painter, option, widget):
pass
def boundingRect(self):
return QtCore.QRectF()
class Edge(QtGui.QGraphicsItem):
def __init__(self):
QtGui.QGraphicsItem.__init__(self)
def paint(self, painter, option, widget):
pass
def boundingRect(self):
return QtCore.QRectF()
if __name__ == '__main__':
import sys
app = QtGui.QApplication(sys.argv)
print QtCore.QT_VERSION_STR, QtCore.PYQT_VERSION_STR
view = QtGui.QGraphicsView()
scene = QtGui.QGraphicsScene()
view.setScene(scene)
scene.addItem(Node())
scene.addItem(Edge())
print scene.items()
view.show()
sys.exit(app.exec_())
</pre>
</blockquote>
<pre wrap=""><!---->
It's because QGraphicsObject inherits both QObject and QGraphicsItem.
items() returns a list of QGraphicsItems which, for a QGraphicsObject, has
a different C++ address than the original QGraphicsObject. PyQt doesn't
recognise that the QGraphicsItem is a cast of the QGraphicsObject. I don't
think there is anything I can (sensibly) do about this.
Phil
</pre>
</blockquote>
</body>
</html>
["Selim_Tuvi.vcf" (text/x-vcard)]
_______________________________________________
PyQt mailing list PyQt@riverbankcomputing.com
http://www.riverbankcomputing.com/mailman/listinfo/pyqt
[prev in list] [next in list] [prev in thread] [next in thread]
Configure |
About |
News |
Add a list |
Sponsored by KoreLogic