2

First of all, I have exported a layer style from a layer as a QML file including draw effects (symbology --> layer rendering --> draw effects) in symbology. Note that, I have checked all the categories while saving it (see below image).

enter image description here

Then, I have changed the values in the field which I have configured a Graduated Symbology with. Then I tried to load the QML file with loadNamedStyle(). It did not change the legend and classes automatically. Therefore, I have tried a few things to automatically update that.

field = 'SPREAD'
stylepath = '/path_to_my_layer_style.qml'
layer = iface.activeLayer()
layer.loadNamedStyle(stylePath)
renderer = layer.renderer()
sym = renderer.sourceSymbol()
grad = renderer.sourceColorRamp()
renderVal = renderer.createRenderer(layer,field,5,0,sym,grad)
layer.setRenderer(renderer)

It worked. However, the effects in that QML file has disappeared after that. I also do not think this is the best way to do that. Is there a more convenient way to classify the symbology with new values after loading QML file in PYQGIS?

1
  • 1
    Sorry they are not called 'special effects' at all. I do not know why I have written like that :) The thing I was trying to indicate is in the symbology --> layer rendering --> draw effects.
    – meteergen
    Commented Dec 15, 2020 at 17:39

1 Answer 1

1

Ok, I found a solution. Here is how I did,

layer.loadNamedStyle(stylePath)
renderer = layer.renderer()

renderer.updateClasses(layer,0,5) # Update the QgsGraduatedSymbologyRenderer.mode and set the classes up to 5
renderer.updateRangeLabels() # Update label range
renderer.updateColorRamp() # Update the color ramp
iface.layerTreeView().refreshLayerSymbology(layer.id()) # Refresh legend on the interface
layer.reload() 

With this way, symbology is being triggered to classify again for the determined parameters and also the symbol effects does not disappear.

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.