<HTML>
<HEAD>
<TITLE>Re: [PyQt] QGraphicsPolygonItem / porting from QCanvas to QGraphicsScene</TITLE>
</HEAD>
<BODY>
<FONT FACE="Calibri, Verdana, Helvetica, Arial"><SPAN STYLE='font-size:11pt'>You are having the same problem but in a slightly different way. It looks like the manual you are using is wrong, btw.<BR>
<BR>
Here are the constructors for QGraphicsPolygonItem:<BR>
<BR>
<a href="http://doc.trolltech.com/4.4/qgraphicspolygonitem.html">http://doc.trolltech.com/4.4/qgraphicspolygonitem.html</a><BR>
<BR>
</SPAN></FONT><FONT COLOR="#174FAF"><FONT FACE="Times, Times New Roman"><SPAN STYLE='font-size:12pt'><B>QGraphicsPolygonItem</B></SPAN></FONT></FONT><FONT FACE="Times, Times New Roman"><SPAN STYLE='font-size:12pt'> ( QGraphicsItem * <I>parent</I> = 0 )<BR>
<FONT COLOR="#174FAF"><B>QGraphicsPolygonItem</B></FONT> ( const QPolygonF & <I>polygon</I>, QGraphicsItem * <I>parent</I> = 0 )<BR>
</SPAN></FONT><FONT FACE="Calibri, Verdana, Helvetica, Arial"><SPAN STYLE='font-size:11pt'><BR>
Note that neither take a scene. I wonder why your manual says that it does?<BR>
<BR>
I always create an item, and <B>then</B> add it to a scene.<BR>
<BR>
First, remove scene from your constructor and from the QGraphicsPolygonItem constructor, then change your code to something like:<BR>
<BR>
vertex = GuiVertex(text)<BR>
self.addItem(vertex)<BR>
<BR>
Brian<BR>
<BR>
On 1/21/09 2:19 PM, "Michael Krauss" <<a href="hippodriver@gmx.net">hippodriver@gmx.net</a>> wrote:<BR>
<BR>
</SPAN></FONT><BLOCKQUOTE><FONT FACE="Calibri, Verdana, Helvetica, Arial"><SPAN STYLE='font-size:11pt'>On 21/01/09 Brian Kelley <<a href="kelley@eyesopen.com">kelley@eyesopen.com</a>> wrote:<BR>
<BR>
> The constructor expects a QGraphicsItem, you are giving it a canvas.<BR>
><BR>
> Note that the symantics of Qcanvas and QGraphicsScene are different.<BR>
<BR>
I have further modified the GuiVertex class now. The code of drawShape<BR>
method is now located in paint. The areaPoints function is gone.<BR>
<BR>
> I hope that this helps.<BR>
<BR>
Indeed, I use your construct with the QPolygonF now.<BR>
<BR>
Here is the complete new code of GuiVertex:<BR>
<BR>
class GuiVertex(QGraphicsPolygonItem):<BR>
'''<BR>
Graphical representation of a vertex<BR>
'''<BR>
def __init__(self, text, scene):<BR>
shape = QPolygonF()<BR>
# quadratic shape<BR>
shape.append(QPointF(0, 0))<BR>
shape.append(QPointF(vertexSize, 0))<BR>
shape.append(QPointF(vertexSize, vertexSize))<BR>
shape.append(QPointF(0, vertexSize))<BR>
<BR>
# test code<BR>
print "type of self: ", type(self)<BR>
print "type of shape: ", type(shape)<BR>
print "type of scene: ", type(scene)<BR>
print "Board.__bases__: ", Board.__bases__<BR>
<BR>
QGraphicsPolygonItem.__init__(self, shape, scene)<BR>
self.text = text<BR>
self.scene = scene<BR>
self.inEdges = []<BR>
self.outEdges = []<BR>
<BR>
def paint(self, painter):<BR>
painter.drawEllipse(self.x(), self.y(), vertexSize, vertexSize)<BR>
# TODO: Find a general approach to place text into the vertex<BR>
painter.drawText(self.x() + 0.35*vertexSize, self.y() +<BR>
0.75*vertexSize, self.text)<BR>
<BR>
<BR>
It still doesn't work:<BR>
<BR>
[mickraus@gandalf src]\$ python gui.py<BR>
type of self: <class '__main__.GuiVertex'><BR>
type of shape: <class 'PyQt4.QtGui.QPolygonF'><BR>
type of scene: <class '__main__.Board'><BR>
Board.__bases__: (<class 'PyQt4.QtGui.QGraphicsScene'>,)<BR>
Traceback (most recent call last):<BR>
File "gui.py", line 424, in <module><BR>
main()<BR>
File "gui.py", line 418, in main<BR>
gui = Gui()<BR>
File "gui.py", line 347, in __init__<BR>
self.board.addVertex(str(v), x, y)<BR>
File "gui.py", line 225, in addVertex<BR>
vertex = GuiVertex(text, self)<BR>
File "gui.py", line 170, in __init__<BR>
QGraphicsPolygonItem.__init__(self, shape, scene)<BR>
TypeError: argument 2 of QGraphicsPolygonItem() has an invalid type<BR>
<BR>
<BR>
The constructors of QGraphicsPolygonItem drive me crazy. The manual<BR>
describes two constructors:<BR>
<BR>
__init__ (self, QGraphicsItem parent = None, QGraphicsScene scene=None)<BR>
__init__ (self, QPolygonF polygon, QGraphicsItem parent = None,QGraphicsScene scene=None)<BR>
<BR>
It tried __init__(shape, self, scene) too but it doesn't work either,<BR>
although it matches the second constructor signature.<BR>
<BR>
<BR>
Kind regards,<BR>
Michael Krauss<BR>
_______________________________________________<BR>
PyQt mailing list <a href="PyQt@riverbankcomputing.com">PyQt@riverbankcomputing.com</a><BR>
<a href="http://www.riverbankcomputing.com/mailman/listinfo/pyqt">http://www.riverbankcomputing.com/mailman/listinfo/pyqt</a><BR>
<BR>
</SPAN></FONT></BLOCKQUOTE>
</BODY>
</HTML>