2

I have 3 layers in the TOC in QGIS. I want to refresh the layers in the TOC to see what layers are present. Because, when I am executing the following code, I am not getting the layers present in QGIS.

layers = canvas.layers()

So I want to refresh the layers present in the TOC and check. How can I do that in QGIS?

0
2

If you want to know which layers are in the layer tree, it's preferable to query the layer tree and not the canvas ;)

QgsProject.instance().layerTreeRoot().findLayers()

QgsProject.layerTreeRoot()

and

QgsLayerTreeGroup.findLayers()

You can then use its layer() method to get the "real" layer

[l.layer() for l in QgsProject.instance().layerTreeRoot().findLayers()]

Or the layer's name() method to get the names

[l.layer().name() for l in QgsProject.instance().layerTreeRoot().findLayers()]
1
  • but i also want the names of the layers present in the toc.
    – user99
    Aug 19 '16 at 13:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.