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...