Graphics Reference
In-Depth Information
painter = context.renderContext().painter()
painter.setPen(self.pen)
painter.drawLine(left, bottom, right, top)
painter.drawLine(right, bottom, left, top)
Using this custom symbol layer in your code is straightforward:
symbol = QgsMarkerSymbolV2.createSimple({})
symbol.deleteSymbolLayer(0)
symbol_layer = CrossSymbolLayer()
symbol_layer.setColor(QColor("gray"))
symbol.appendSymbolLayer(symbol_layer)
Running this code will draw a cross at the location of each point geometry, as follows:
Of course, this is a simple example, but it shows you how to use custom symbol layers
implemented in Python. Let's now take a closer look at the implementation of the
CrossSymbolLayer class, and see what each method does:
__init__() : Notice how the __init__ method accepts parameters that cus-
tomize the way the symbol layer works. These parameters, which should always
have default values assigned to them, are the properties associated with the sym-
bol layer. If you want to make your custom symbol available within the QGIS
Layer Properties window, you will need to register your custom symbol layer
and tell QGIS how to edit the symbol layer's properties. We will look at this
shortly.
layerType() : This method returns a unique name for your symbol layer.
properties() : This should return a dictionary that contains the various prop-
erties used by this symbol layer. The properties returned by this method will be
stored in the QGIS project file, and used later to restore the symbol layer.
Search WWH ::




Custom Search