I am trying to modify the data source of a raster layer in python, then update my composer and finally save it.
To modify my data source I found what is needed here: https://geogear.wordpress.com/2015/05/15/changing-qgis-layer-datasource-with-python/
The problem is that I lose the symbology of this layer in the legend of my composer. I tried different codes but none update my composer legend :
iface.activeComposers()[0].composition().refreshItems()
for i in iface.activeComposers()[0].composition().items():
if isinstance(i,QgsComposerLegend):
legend = i
legend.updateLegend()
The only solution I found at the moment is to use the "setAutoUpdateModel" method on my legend and switch to True / False Auto Update to force the legend's update, but this solution is not optimal.
=> I wish I could just update the symbology for a particular layer in the legend of my compose (a bit like when in the interface of my composer I click on my legend then in "property of the object" right clicks on my layer and "back to default").
Would anyone have an idea to do that?
EDIT :
I have found a solution here : Changing legend item text with pyQGIS?
My new code :
for legendLyr in iface.mapCanvas().layers():
renderer = legendLyr.renderer()
myRenderer = renderer.clone()
legendLyr.setRenderer(myRenderer)
legendLyr.triggerRepaint()
Now my composer legend is refresh !