In my plugin I want to select some layers programmatically. But with

virtual QList<QgsMapLayer *> QgsLegendInterface::selectedLayers(bool inDrawOrder = false)

I only receive the actually selected layers.

I've tried in QgsLayerTreeView the method void QgsLayerTreeView::setCurrentLayer(QgsMapLayer * layer) But I only can select one Layer, and I need to select more layers.

Any idea what could I try?

1 Answer 1


I couldn't find the way directly from the QGIS API. However, you can achieve it using Qt4.

You need to temporarily set the selection mode of the layer tree view to multi-selection, select all your layers, and then set the selection mode back.

from PyQt4.QtGui import QAbstractItemView
iface.layerTreeView().setSelectionMode( QAbstractItemView.MultiSelection )

# Select your layers, this time they are not mutually exclusive

iface.layerTreeView().setSelectionMode( QAbstractItemView.ExtendedSelection )

For QGIS 3, replace from PyQt4.QtGui import QAbstractItemView with from qgis.PyQt.QtWidgets import QAbstractItemView to use PyQt5


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.