Placing Text Annotation on Map Canvas

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Placing Text Annotation on Map Canvas

mmekuria
This post has NOT been accepted by the mailing list yet.
I am sure if there is a way to remove text annotation there must first be a way to add them.
I want to be able to add/update a text annotation on the canvas using python. I again miss my C++ where it is much easier to follow the API documentation.  But Python, it is always a hit and miss because of gaps in the documentation.  I have tried a bit and seem to come short of what I should do to add/update a text annotation dynamically while creating images of the map layers.

I am using QgsTextAnnotationItem  as shown below

canvas = qgis.utils.iface.mapCanvas()
m = QgsTextAnnotationItem(canvas)
dctext = QTextDocument('Date and Time - 10012014 Epoch 95')
m.setDocument(dctext)
m.setRect(QgsRectangle(300,500,500,700))
m.show()

Then I get lost. Any documentation on how to put the annotation on the canvas.   I scrubbed the API and it there doesn't seem to be anything besides symbols (QgsMarkerSymbol) which would be only perhaps for the border or other symbols.
 
Maaza Christos, PhD
www.Axumcorp.com
Reply | Threaded
Open this post in threaded view
|

Re: Placing Text Annotation on Map Canvas

mmekuria
This post has NOT been accepted by the mailing list yet.
This post was updated on .
Thank you to all who viewed this posting. But I was able to find out the way to do it. Those who did not view or answer it and to all,  I wish all a very Merry Christmas.  

I found the answer somehow I thought the canvas was working on pixel coordinates and I was giving it coordinate that are very high (over 300) and it was not showing up.  I used the coordinates of the canvas extent and now I see the text.


from qgis.gui import *
from PyQt4.QtGui import *
from PyQt4.QtCore import *
canvas = qgis.utils.iface.mapCanvas()
sc = canvas.scene()
mx = canvas.extent()
anit = QgsTextAnnotationItem(canvas)
anit.setMapPosition(mx.center())
dctext = QTextDocument("Date and Time  ")
anit.setDocument(dctext)
sc.addItem(anit)
canvas.refresh()


And one can remove it with
sc.removeItem(anit)

one can update by just updating the pointer anit
dctext.setPlainText( " new date ")
anit.setDocument(dctext)
canvas.refresh()



Maaza Christos, PhD
www.Axumcorp.com