2

I have been looking for a QGIS toggle to disable layers' feature selection like in ArcMap/MapInfo. Found Setting layer as unselectable to avoid identifying features in multiple layers using QGIS but answers were only about feature identification.

Recently tested pyqgis snippet that completely disables manual feature selection in map area. Also wrote about it in answer area.

iface.activeLayer().selectionChanged.disconnect()

This thing will remove a signal for a selection of current layer's features. But now I can't understand how to bring the selection signal back. Tried to connect a signal from other layer but no results, though I can't clearly understand pyqtSignal logic here. Any thoughts?

2

QGIS has no setting for disabling layer's feature selection. But, of course, you can do it using PyQGIS.

Disabling feature selection on a layer

def clearSelection(param1, param2, param3):
    layer.removeSelection()

layer.selectionChanged.connect(clearSelection)

Enabling feature selection again

layer.selectionChanged.disconnect(clearSelection)

Result

enter image description here


Why not using disconnect()? (As you suggest in the question)

Don't do it using the disconnect() (disconnect all), because you will break some things in your QGIS, for sure.

Try this search, and when you see that a &QgsVectorLayer::selectionChanged SIGNAL is being connected, then you need to get access to the SLOT's object to reconnect that SLOT to your SIGNAL. If you don't do that for all the SLOTs, your QGIS won't work as it was working before calling the dangerous disconnect() (disconnect all).

5
  • yeah works really nice, but I though that there could be a prime solution, like connecting another function or signal to return initial selection function Jun 1 at 14:48
  • It's not a good practice to disconnect everything from a Qt object, since it could be connected to a number of SLOTS that we potentially don't know. If you want to "connect it" again, you should then keep track of all those SLOTS beforehand. So, in this case, the recommended solution is to use a single function (the custom one above) that allows to to get back to your initial state, without messing things up. Jun 1 at 15:01
  • that really makes a sense. Anyway with a command iface.activeLayer().selectionChanged.signal I've seen the string 2selectionChanged(QgsFeatureIds,QgsFeatureIds,bool). Thought there is a way to reconnect this signal Jun 1 at 15:07
  • You could of course reconnect it. The question is "to what?". Since QGIS is a complex app, you would need to know all the SLOTS the SIGNAL was connected to, and also have access to all their corresponding objects. Only then you could reconnect it by yourself. Jun 1 at 15:17
  • Just wanted to see if it's not too complex :) As far as I know lots of settings in QGIS can be changed by pyqgis so I thought that even selectionChanged is not too complicated thing to get a manual set/reset Jun 2 at 6:06

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.