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 exempleQgsSymbolLineSymbolV
has become QgsLineSymbol
and setRendererV2
which becomes setRenderer
. So, with the help of this blog this GitHubthe API and the most recent doc 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 doc and otherwise tried to remodulate the code a bit but I'm a bit stuck (i'm still learning Python).
Best Answer
In QGIS 3.4.x is: