2

In QGIS 2.x, I used the following code to refresh a graduated symbology legend in the layers panel:

self.iface.legendInterface().refreshLayerSymbology(self.myLayer)

The new QGIS 3 API advises to use QgsLayerTreeNode instead, but the only way I found to get it working is the following code :

root = QgsProject.instance().layerTreeRoot()
model = self.iface.layerTreeView().model()
model.refreshLayerLegend(root.findLayer(self.myLayer.id()))

... which looks rather cumbersome. What am I missing ?

2 Answers 2

5

Just use this:

iface.layerTreeView().refreshLayerSymbology(layer.id())
1
  • 1
    Thank you so much, Gary, I am very proud to get an answer from QGIS project founder himself !
    – sigeal
    Mar 1, 2018 at 6:10
2

your code is correct,maybe you can use two lines only,for example.

node = QgsProject.instance().layerTreeRoot().findLayer(self.myLayer.id())
self.iface.layerTreeView().layerTreeModel().refreshLayerLegend(node)

it's a proposal

1
  • In QGIS 3.2.2 this worked for me.
    – taudorf
    Oct 4, 2018 at 12:48

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.