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().setCurrentLayer(layer1)
iface.layerTreeView().setCurrentLayer(layer2)
iface.layerTreeView().setCurrentLayer(layer3)
iface.layerTreeView().setSelectionMode( QAbstractItemView.ExtendedSelection )
For QGIS 3, replace from PyQt4.QtGui import QAbstractItemView
with from qgis.PyQt.QtWidgets import QAbstractItemView
to use PyQt5
Explore related questions
See similar questions with these tags.