QGIS 3.36.2 with Python 3.12.3
Users draw a QgsAnnotationLineItem
or QgsAnnotationPolygonItem
on a QgsAnnotationLayer
my script creates, once they're done and press a button, the script reads the item's coordinates. However, QgsAnnotationLineItem
draws a really thin, light red polyline, which is quite hard to see, so I want to change not only the default width but also the default color for all polylines and the default line and fill color for all polygons drawn by the user.
With a regular QgsVectorLayer
I'd create a QgsMarkerSymbol
, modify it and its QgsSymbolLayer
however I want, then assign it to the layer's QgsFeatureRenderer
. QgsAnnotationLayer
doesn't contain a renderer like that, instead it only inherits a QgsAbstract3DRenderer
, which is probably not the right thing to use (no matching functions, according to the docs).
I thought about creating a polygon and polyline myself to modify them and delete them again afterwards, hoping that that would set the default style:
self.layer = QgsAnnotationLayer(self.layername,QgsAnnotationLayer.LayerOptions(QgsProject.instance().transformContext()))
self.layer.setCrs(self.crs)
line = QgsAnnotationLineItem.create()
geo1 = QgsLineString(pointarray1)
line.setGeometry(geo1)
symbol1 = line.symbol() #QgsLineSymbol
symbol1.setWidth(1) #Default: 0.26
symbol1.setColor(QColor(0,100,255,255)) #Blue
self.layer.addItem(line)
polygon = QgsAnnotationPolygonItem.create()
geo2 = QgsPolygon(QgsLineString(pointarray2))
polygon.setGeometry(geo2)
symbol2 = polygon.symbol() #QgsFillSymbol
symbollayer = symbol2.symbolLayer(0) #QgsSimpleFillSymbolLayer
symbollayer.setFillColor(QColor(0,255,0,255)) #Fill Color = Inside #Green
symbollayer.setStrokeColor(QColor(255,0,255,255)) #StrokeColor = Border #Magenta
symbollayer.setStrokeWidth(0.5) #Default: 0.26
self.layer.addItem(polygon)
QgsProject.instance().addMapLayer(self.layer)
self.layer.reset()
This draws a polygon and polyline in the correct colors and instantly removes them again but it seems to be pretty random if this then also affects the color,... of the annotations I draw manually (as the user would). The few times it did work (only three times out of many tries so far), only the polyline used the correct settings, the polygon always used the default, thin, black border, instead of the custom stroke color and width I'd set.
How do I programmatically set the default style for all polygons or poly lines my users draw?