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?
1 Answer 1
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
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).
-
yeah works really nice, but I though that there could be a prime solution, like connecting another function or signal to return initial selection functionPavel Pereverzev– Pavel Pereverzev2021年06月01日 14:48:45 +00:00Commented Jun 1, 2021 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.Germán Carrillo– Germán Carrillo2021年06月01日 15:01:55 +00:00Commented Jun 1, 2021 at 15:01
-
that really makes a sense. Anyway with a command
iface.activeLayer().selectionChanged.signal
I've seen the string2selectionChanged(QgsFeatureIds,QgsFeatureIds,bool)
. Thought there is a way to reconnect this signalPavel Pereverzev– Pavel Pereverzev2021年06月01日 15:07:05 +00:00Commented Jun 1, 2021 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.Germán Carrillo– Germán Carrillo2021年06月01日 15:17:57 +00:00Commented Jun 1, 2021 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/resetPavel Pereverzev– Pavel Pereverzev2021年06月02日 06:06:32 +00:00Commented Jun 2, 2021 at 6:06