I'm currently updating some Python code from QGIS 2.18 to QGIS 3.4
in 2.18 I used the following code :
#First off I created a virtual layer using an sql query here
vlayer = QgsVectorLayer( "?query=....", "virtual" )
# Then I assign the properties of the polyline layer here
props = { 'width' : '3', 'color' : '255,0,0' }
sl = QgsSymbolLayerV2Registry.instance().symbolLayerMetadata("SimpleLine").createSymbolLayer(props)
s = QgsLineSymbolV2([sl]) vlayer.setRendererV2( QgsSingleSymbolRendererV2( s ) )
I know there have been a number of changes in QGIS 3, for example QgsSymbolLineSymbolV
has become QgsLineSymbol
and setRendererV2
has become setRenderer
. So, with the help of this blog, this GitHub repo, the API and the most recent docs on the QGIS site, I've put the script below together:
layer = QgsProject.instance().mapLayersByName("t_alpha")[0]
single_symbol_renderer = layer.renderer()
symbol = single_symbol_renderer.symbol()
symbol.setColor(QColor.fromRgb(255, 0, 0))
symbol.setWidth(3)
layer.triggerRepaint()
qgis.utils.iface.layerTreeView().refreshLayerSymbology(layer.id())
But this returns an error:
AttributeError: 'QgsCategorizedSymbolRenderer' object has no attribute 'symbol'
I've dug through the docs and otherwise tried to remodulate the code but I'm a bit stuck (I'm still learning Python).