[PyQt] QGraphicsPolygonItem / porting from QCanvas to QGraphicsScene

Brian Kelley kelley at eyesopen.com
Wed Jan 21 14:05:06 GMT 2009


The constructor expects a QGraphicsItem, you are giving it a canvas.

Note that the symantics of Qcanvas and QGraphicsScene are different.

Here is a stupid example:

class HideButton(QtGui.QGraphicsPolygonItem):
    fgcolor = QtGui.QColor(20,20,20, 180)
    bgcolor = QtGui.QColor(180,180,180)

    def __init__(self, parent):
        p = QtCore.QPointF
        points = QtGui.QPolygonF()
        // these points are relative to the parent
        for poly in (p(-6,-6),p(6,-6),p(0,6)):
            points.append(poly)

        QtGui.QGraphicsPolygonItem.__init__(self, points, parent)
        self.setBrush( QtGui.QBrush( self.bgcolor  ) )
        self.setPen( QtGui.QPen(self.fgcolor,2) )

Now we can add a hide button as a child of another graphics item...

  self.button = HideButton(item)

And this will take care of rendering with the parent is visible.

To add the top level item to the scene:

  scene.addItem( item )

And then make sure to add this scene to your view.

I hope that this helps.
On 1/21/09 8:38 AM, "Michael Krauss" <hippodriver at gmx.net> wrote:

Hello list members,

i am porting a program from PyQt3 to PyQt4. It formerly used QCanvas
and QCanvasView. Now i am using QGraphicsScene and QGraphicsView.

I replaced QCanvasPolygonalItem with QGraphicsPolygonItem but its
constructor doesn't work as expected.

The old code:

class GuiVertex(QCanvasPolygonalItem):
    '''
    Graphical representation of a vertex
    '''
    def __init__(self, text, canvas):
        QCanvasPolygonalItem.__init__(self, canvas)
[...]

The new code:

class GuiVertex(QGraphicsPolygonItem):
    '''
    Graphical representation of a vertex
    '''
    def __init__(self, text, canvas):
        QGraphicsPolygonItem.__init__(self, canvas)
[...]

The error message:

[mickraus at gandalf src]\$ python gui.py
Traceback (most recent call last):
  File "gui.py", line 420, in <module>
    main()
  File "gui.py", line 414, in main
    gui = Gui()
  File "gui.py", line 343, in __init__
    self.board.addVertex(str(v), x, y)
  File "gui.py", line 221, in addVertex
    vertex = GuiVertex(text, self)
  File "gui.py", line 157, in __init__
    QGraphicsPolygonItem.__init__(self, canvas)
TypeError: argument 1 of QGraphicsPolygonItem() has an invalid type

According to the manual
http://www.riverbankcomputing.co.uk/static/Docs/PyQt4/html/qgraphicspolygonitem.html
the constructor expects a QGraphicsItem. As GuiVertex is a (sub)^4class
of QGraphicsItem I don't understand the TypeError. The second argument
passed to QGraphicsPolygonItem.__init__ in line 221 is of type
"class Board(QGraphicsScene)".


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/7dac8a79/attachment.html


More information about the PyQt mailing list