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?


This would be enough:

| improve this answer | |

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:


more detail exploring the class QgsLayerTreeView:


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

| improve this answer | |
  • I tried layers = qgis.utils.iface.layerTreeRoot().selectedLayers() but I get en error "QgisInterface object has no attribute layerTreeRoot" – swooders Mar 10 '15 at 17:23
  • sorry I erroneously wrote the wrong method... it is layerTreeView that can be simply read from QgisInterface documentation. – Luigi Pirelli Mar 10 '15 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 Mar 11 '15 at 20:29

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.