[PyQt] QGraphicsPolygonItem / porting from QCanvas to QGraphicsScene

Michael Krauss hippodriver at gmx.net
Wed Jan 21 19:19:39 GMT 2009


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


More information about the PyQt mailing list