1

I am trying to get my plugin working and have some issues with the count of currently selected layers.

As am listening to legendInterface().currentLayerChanged(), triggering a function that is returning the selected layers. If the length of the list returned is not 1 then I am disabeling a range of widgets in my dock.

However, it does not work properly since the count of selected layers seems to be of. If there is no layer selected when the plugin is initialized then with the selection of the first layer the length of the list returned from len(utils.iface.legendInterface().selectedLayers()) is actually zero! If another layer is selected afterwards the length of the list returned becomes one!? That leads to the problem that even when I select an additional layer (2 selected) then the returned length of the list is again one, and all the widgets that I want to disable stay enabled. Only if I select three layers the behaviour becomes correct.

Here the code I am using. Maybe it's the wrong signal to listen to, but I do not know better at the moment. How can I get the number of layers returned right so it actually reflects the number of selected layers in the TOC?

def __init__(self, parent=None):
    self.setupUi(self)
    utils.iface.legendInterface().currentLayerChanged.connect(self.changeLayer)

def changeLayer(self):
    utils.iface.messageBar().pushMessage("Info", "Numer: "+str(len(utils.iface.legendInterface().selectedLayers())), level=gui.QgsMessageBar.CRITICAL)
    if not len(utils.iface.legendInterface().selectedLayers()) == 1:
        self.lbl_active_layer_value.setText("None or more than one layer selected!")
        self.btn_set_subset.setDisabled(True)
        self.cbb_mark_visual.setDisabled(True)
        self.cbb_mark_set.setDisabled(True)
    else:
        cur_layer = utils.iface.legendInterface().currentLayer()
        self.lbl_active_layer_value.setText(cur_layer.name())
        layer_name = cur_layer.name()
        self.btn_set_subset.setEnabled(True)
        self.cbb_mark_visual.setEnabled(True)
        self.cbb_mark_set.setEnabled(True)

I am using QGIS 2.18.16...

  • Are you perchance selecting a group of layers, instead of the individual layers? Do you observe this behaviour with any set of layers, or a specific set – Roberto Ribeiro Feb 11 '18 at 19:11
  • I have currently four layers in my project, not a single layer group. I bound the same function before to the signal of a pushButton and there I haven't had this problem at all. Only when I changed to the currentLayerChanged signal this started. – Thomas Becker Feb 11 '18 at 19:14
1

I think you need to connect to the selectionChanged signal o the layer tree selection model...

def change2():
  iface.messageBar().pushMessage("Info", "Selected..."+str(len(utils.iface.legendInterface().selectedLayers())), level=qgis.gui.QgsMessageBar.CRITICAL)

Get the layer tree view:

v = iface.layerTreeView()

Connect to the selection model:

v.selectionModel().selectionChanged.connect(change2)

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.