3

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?

Germán Carrillo
37.3k5 gold badges127 silver badges182 bronze badges
asked Sep 26, 2016 at 8:54

1 Answer 1

3

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

ThomasG77
31.7k1 gold badge56 silver badges96 bronze badges
answered Sep 26, 2016 at 13:21
0

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.