I'd like to change the symbology of my vector layer and can't figure it out how to read the documentation. I would like to have an polygone with an orange outline, a dashed fill style and an orange stroke color. However that would be nice to have an answer that explains how to access most of the symbology change! (if possible 😉 )
Here is what I managed so far:
import os
import processing
from qgis.core import *
from qgis.gui import *
import qgis.utils
# initialize project
project = QgsProject.instance()
project.setCrs(QgsCoordinateReferenceSystem(21781))
project.setFileName("C:\\test_symbology.qgs")
my_shp = "Y:\\shp_collection\\A300.shp"
my_shp_name = "A300"
# load polygon
shplayer = iface.addVectorLayer(my_shp , my_shp_name , "ogr")
if not shplayer.isValid():
print(".shp failed to load!")
shplayer.setCrs( QgsCoordinateReferenceSystem(21781,QgsCoordinateReferenceSystem.EpsgCrsId))
layer = iface.activeLayer()
# make my polygone orange
single_symbol_renderer = layer.renderer()
symbol = single_symbol_renderer.symbol()
symbol.setColor(QColor.fromRgb(255,128,0))
How can I set the other symbology properties?
Best Answer
The easiest for me would be to check the properties of the symbology layer which contains symbol properties affecting the whole layer:
To access individual symbol layers inside, you could use:
Near the end of this list of properties, we can see there's settings for
setBrushStyle
andsetStrokeColor
. ThesetBrushStyle
requiresQt.BrushStyle
to set a brush style, we can check the properties insideQt
using:So doing a little search, we can find brush patterns such as
BDiagPattern
orFDiagPattern
So now we can apply the relevant settings: