qgis-sample-QgsHighlight.py
from PyQt4.QtCore import QVariant
from PyQt4.QtGui import QColor
from qgis.core import (QgsFeature, QgsField,
QgsGeometry, QgsPoint,
QgsMapLayerRegistry,
QgsVectorLayer)
from qgis.gui import QgsHighlight
from qgis.utils import iface
vl = QgsVectorLayer("Point?crs=epsg:4326&index=yes",
"temporary_points", "memory")
pr = vl.dataProvider()
pr.addAttributes([
QgsField("name", QVariant.String),
QgsField("age", QVariant.Int),
QgsField("size", QVariant.Double)
])
vl.updateFields()
infos = [
[10, 10, "John", 24, 1.73],
[40, -60, "Paul", 29, 1.86],
[60, 5, "George", 34, 1.69],
[0, 45, "Ringo", 73, 1.75]
]
for i in infos:
fet = QgsFeature()
fet.setGeometry(QgsGeometry.fromPoint(QgsPoint(i[0], i[1])))
fet.setAttributes(i[2:5])
pr.addFeatures([fet])
vl.updateExtents()
QgsMapLayerRegistry.instance().addMapLayer(vl)
highlight = QgsHighlight(
iface.mapCanvas(),
QgsGeometry.fromPoint(QgsPoint(0, 47)),
vl
)
highlight.setBuffer(1.5)
highlight.setColor(QColor('black'))
highlight.setFillColor(QColor('blue'))
highlight.setWidth(0.5)
iface.mapCanvas().refresh()