3

qgis.utils.iface.activeLayer() accesses the current active layer, but I would like to get all checked layers in the QGIS ToC.

How can I do it?

2 Answers 2

3

This would be enough:

iface.mapCanvas().layers()
1

in your case qgis.utils.iface.activeLayer() return the last selected one. To obtain selected layers use the ability of the TreeView that contain the layer list. More directly:

iface.layerTreeView().selectedLayers()

more detail exploring the class QgsLayerTreeView:

http://qgis.org/api/classQgsLayerTreeView.html

obviously this work after legend refactoring => qgis >= 2.6

3
  • I tried layers = qgis.utils.iface.layerTreeRoot().selectedLayers() but I get en error "QgisInterface object has no attribute layerTreeRoot"
    – swooders
    Commented Mar 10, 2015 at 17:23
  • sorry I erroneously wrote the wrong method... it is layerTreeView that can be simply read from QgisInterface documentation. Commented Mar 10, 2015 at 21:58
  • Sorry I actually meant the layers that were checked (not selected). I apologize for being unclear in the question - is there a method that gets the layers with checked boxes?
    – swooders
    Commented Mar 11, 2015 at 20:29

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.