[PyQt] QGraphicsPolygonItem / porting from QCanvas to QGraphicsScene

Brian Kelley kelley at eyesopen.com
Wed Jan 21 19:30:33 GMT 2009


You are having the same problem but in a slightly different way.  It looks like the manual you are using is wrong, btw.

Here are the constructors for QGraphicsPolygonItem:

http://doc.trolltech.com/4.4/qgraphicspolygonitem.html

QGraphicsPolygonItem ( QGraphicsItem * parent = 0 )
QGraphicsPolygonItem ( const QPolygonF & polygon, QGraphicsItem * parent = 0 )

Note that neither take a scene.  I wonder why your manual says that it does?

I always create an item, and then add it to a scene.

First, remove scene from your constructor and from the QGraphicsPolygonItem constructor, then change your code to something like:

 vertex = GuiVertex(text)
 self.addItem(vertex)

Brian

On 1/21/09 2:19 PM, "Michael Krauss" <hippodriver at gmx.net> wrote:

On 21/01/09 Brian Kelley <kelley at eyesopen.com> wrote:

> The constructor expects a QGraphicsItem, you are giving it a canvas.
>
> Note that the symantics of Qcanvas and QGraphicsScene are different.

I have further modified the GuiVertex class now. The code of drawShape
method is now located in paint. The areaPoints function is gone.

> I hope that this helps.

Indeed, I use your construct with the QPolygonF now.

Here is the complete new code of GuiVertex:

class GuiVertex(QGraphicsPolygonItem):
    '''
    Graphical representation of a vertex
    '''
    def __init__(self, text, scene):
        shape = QPolygonF()
        # quadratic shape
        shape.append(QPointF(0, 0))
        shape.append(QPointF(vertexSize, 0))
        shape.append(QPointF(vertexSize, vertexSize))
        shape.append(QPointF(0, vertexSize))

        # test code
        print "type of self: ", type(self)
        print "type of shape: ", type(shape)
        print "type of scene: ", type(scene)
        print "Board.__bases__: ", Board.__bases__

        QGraphicsPolygonItem.__init__(self, shape, scene)
        self.text = text
        self.scene = scene
        self.inEdges = []
        self.outEdges = []

    def paint(self, painter):
        painter.drawEllipse(self.x(), self.y(), vertexSize, vertexSize)
        # TODO: Find a general approach to place text into the vertex
        painter.drawText(self.x() + 0.35*vertexSize, self.y() +
        0.75*vertexSize, self.text)


It still doesn't work:

[mickraus at gandalf src]\$ python gui.py
type of self:  <class '__main__.GuiVertex'>
type of shape:  <class 'PyQt4.QtGui.QPolygonF'>
type of scene:  <class '__main__.Board'>
Board.__bases__:  (<class 'PyQt4.QtGui.QGraphicsScene'>,)
Traceback (most recent call last):
  File "gui.py", line 424, in <module>
    main()
  File "gui.py", line 418, in main
    gui = Gui()
  File "gui.py", line 347, in __init__
    self.board.addVertex(str(v), x, y)
  File "gui.py", line 225, in addVertex
    vertex = GuiVertex(text, self)
  File "gui.py", line 170, in __init__
    QGraphicsPolygonItem.__init__(self, shape, scene)
TypeError: argument 2 of QGraphicsPolygonItem() has an invalid type


The constructors of QGraphicsPolygonItem drive me crazy. The manual
describes two constructors:

__init__ (self, QGraphicsItem parent = None, QGraphicsScene scene=None)
__init__ (self, QPolygonF polygon, QGraphicsItem parent = None,QGraphicsScene scene=None)

It tried __init__(shape, self, scene) too but it doesn't work either,
although it matches the second constructor signature.


Kind regards,
Michael Krauss
_______________________________________________
PyQt mailing list    PyQt at riverbankcomputing.com
http://www.riverbankcomputing.com/mailman/listinfo/pyqt

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.riverbankcomputing.com/pipermail/pyqt/attachments/20090121/9fa7f974/attachment.html


More information about the PyQt mailing list