1

I am currently writing a QGIS 3 plugin. I would like to catch the signal emmited each time the user selects a new layer in the layer panel.

I have already searched in the QGIS API (QgsProject, QgsLayerTree, QgsLayerTreeView...)

QgsLayerTreeView has a signal currentLayerChanged but I cannot find how to connect QgsLayerTreeView to QgsProject.instance().

Does anyone has an idea on what is the solution or at least where I can find some information?

1

You can connect this signal to iface for example.

def currentLayerChanged(layer):
    print(layer.name())

iface.currentLayerChanged.connect(currentLayerChanged)

Or if you want make connection using QgsLayerTreeView

def onChange(layer):
  print("Current Layer: " + str(layer.name()))

# connect to the signal
view = iface.layerTreeView()
view.currentLayerChanged.connect(onChange)

Check the lutraconsulting blog for more information. Note that this site use QGIS 2.4

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.