How can I refresh the Layers Panel in QGIS through python?
In my code I have loop and within line to update visibility of some layer in the legend:
self.iface.legendInterface().setLayerVisible( layer, True )
At the beginning I thought it should do that that automatically. But it doesn't. Very interesting thing for me is that if I have open Python Console and print something after that line the Layers Panel gets updated. If the Python Console is closed nothing happens.
My intention is to build plugin witch loop through layers in selected group and for every layer turns it on (the rest in group turn off) and export composer. The whole problem is that after switching visibility of layers the Layers Panel won't update automatically and so also the composer is not updated and gets exported without any change in the layers visibility. Just accidentally I have found out that if I open Python Console and with every loop print out something not only Python Console gets updated but for some reason also Layers Panel and so Composer and so the export works as needed. But I feel it would be stupid to say if you want my plugin to work correctly you need to open python console... There must be some trigger or something to force Layers Panel to update. What is it? :)
print
line after your loop?self.iface.legendInterface().refreshLayerSymbology(layer)
updateLegend()
method, like this:from qgis.core import QgsComposerLegend
for item in composer.composition().items():
if isinstance(item, QgsComposerLegend):
item.updateLegend()
break