[Qgis-developer] custom map canvas item?

Ricardo Filipe Soares Garcia da ricardo.garcia.silva at gmail.com
Fri Aug 13 12:19:07 EDT 2010


Hi list
How could I go about defining a custom Map Canvas Item in Python?

In addition to the current QgsRubberBand and QgsVertexMarker I'd like
to have a new Map Canvas Item that could write text on the map canvas.

I am thinking about creating a plugin to display a raster's value
directly on the canvas, as if it were a matrix of numbers, and thought
that a new Map Canvas Item, kind of like QgsVertexMarker, but that
would allow for drawing text instead of a symbol, would do the trick.

I tried to subclass QgsVertexMarker and create a new method for
drawing text, using a QPen object, but it didn't work... I guess I
have to tell the QPen where to draw, but I don't know how to do it. If
someone could just give me a small push I think I could manage the
rest.

Thanks


#my (broken) code
class MyMarker(qgis.gui.QgsVertexMarker):
    def __init__(self, canvas):
        qgis.gui.QgsVertexMarker.__init__(self, canvas)

    def write_text(self, painter, text, x, y):

        pen = QPen(QColor(0,0,0))
        pen.setWidth(1)
        painter.setPen(pen)
        painter.drawText(x, y, text)

mc = iface.mapCanvas()
p = QPainter()
mark = MyMarker(mc)
mark.write_text(p, "testing...", 0, 0) #nothing happens

-- 
___________________________ ___ __
Ricardo Garcia Silva


More information about the Qgis-developer mailing list