3

I'm trying to customize my layers from a python script. I would like to add several symbol layers to features.

# Creating symbol layers
symbolLayers = list()
lFill = QgsSimpleFillSymbolLayer.create({
    "color" : "{r},{g},{b},255".format(r = randomized_color[0],g = randomized_color[1],b = randomized_color[2])
})
symbolLayers.append(lFill)

# Create the fill symbol
symbol = QgsFillSymbol(symbolLayers)

# Adding to the current renderer
currentLayer.renderer().setSymbol(symbol)
currentLayer.triggerRepaint()

I have at this time only one symbol layer but when it will work, i will add more symbol layers.

It crash only when i add the symbol to the renderer. Before, it seems to work (setSymbol).

1 Answer 1

3

The solution was not so hard to find. If someone need the same :

# Must to create from a defaultsymbol
symbol = QgsSymbol.defaultSymbol(currentLayer.geometryType())
symbol.deleteSymbolLayer(0)

# Create all style layers
lFill = QgsSimpleFillSymbolLayer.create({
    "outline_color" : "{r},{g},{b},255".format(r = randomized_color[0],g = randomized_color[1],b = randomized_color[2]),
    "outline_width" : "1",
    "style" : "no"
})
lFill2 = QgsSimpleFillSymbolLayer.create({
    "outline_style" : "no",
    "color" : "{r},{g},{b},90".format(r = randomized_color[0],g = randomized_color[1],b = randomized_color[2])
})

# Add them
symbol.appendSymbolLayer(lFill)
symbol.appendSymbolLayer(lFill2)

# Create a renderer with the symbol as first parameter
renderer = QgsSingleSymbolRenderer(symbol)

# Define the renderer
currentLayer.setRenderer(renderer)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.