I'm trying to automate adding styles to layers, with each layer having multiple styles using pyqgis.
Couldn't find any answers except this unanswered question:
I did manage to add multiple styles using QgsMapLayerStyleManager:
layer = iface.activeLayer() layer.name() mlsm = QgsMapLayerStyleManager(layer) layer.loadNamedStyle('~/Styles/BasinAnalysis/Area/Total.qml') mlsm.renameStyle("default","Area-Total") mlsm.addStyleFromLayer("Area-Freshwater") layer.loadNamedStyle('~/Styles/BasinAnalysis/Area/Freshwater.qml') mlsm.addStyleFromLayer("Area-Unirrigated") layer.loadNamedStyle('~/Styles/BasinAnalysis/Area/Unirrigated.qml') etc...
It seems like the multiple styles are associated to layer, and I can also see the changes applies to layer:
mlsm.styles() >>> ['Area-Freshwater', 'Area-Other', 'Area-Saline', 'Area-TWW', 'Area-Total', 'Area-Unirrigated']
The problem is that styles don't appear when I right click on Layer -> Styles, it only shows the default layer.
I'm guessing I need to associate the styles from the StyleManager back to the layer, but not sure how.