Goal: The snapping functionality should be enabled and disabled.
I use the function setSnapSettingsForLayer. Convenience function to set snap settings per layer. Short Example:
QgsProject.instance().setSnapSettingsForLayer(layer.id(), True, 2, 1, 10, True)
This approach has already been explained in another post: How to enable the snapping for a layer with the tolerance value with python programming
Up to this point everything works. Now we come to the problem. How can I disable the snapping functionality?
If I set the second parameter in the function setSnapSettingsForLayer to False (to enable the layer snapping), then nothing happens. The Snapping remains activated. Even if I change an another parameter e.g. tolerance, also does not change.
So, how can the Snapping be enabled and disabled while editing?
UPDATE:
Despite my answer, the problem is not yet solved. By choosing Settings -> Snapping options you will find the following window
Only when I confirm this window with the 'OK' button, the code in my answer works. Only now the Snapping can be turned off and on during processing. Has anyone any idea?
-
What version of QGIS are you using?artwork21– artwork212016年06月03日 12:43:32 +00:00Commented Jun 3, 2016 at 12:43
-
Other related problem: gis.stackexchange.com/questions/195760/…WKT– WKT2016年06月03日 13:11:47 +00:00Commented Jun 3, 2016 at 13:11
-
@artwork21 Version 2.14.0 Esseneftas– eftas2016年06月07日 06:10:19 +00:00Commented Jun 7, 2016 at 6:10
2 Answers 2
I seem to have found the problem. In QGIS all layers were initially activated for the Snapping. First I disable any snapping operations. Subsequently, the Snapping is enabled for a few layers.
for item in QgsMapLayerRegistry.instance().mapLayers().values():
QgsProject.instance().setSnapSettingsForLayer(item.id(), False, 2, 0, 2, True)
fieldLayer = mapUtils.getLayerByName(agroInstance.dictData['lpisid'])
backgroundLpisLayer = mapUtils.getLayerByName('LPISe')
if checked:
# Convenience function to set snap settings per layer.
# it defines the snapping options:
# id : the id of your layer, True : to enable the layer snapping, 2 : options (2: vertex+segment), 0: type of unit on map, 2 : tolerance, true : avoidIntersection)
QgsProject.instance().setSnapSettingsForLayer(layerA.id(), True, 2, 0, 2, True)
QgsProject.instance().setSnapSettingsForLayer(layerB.id(), True, 2, 0, 2, True)
The variable 'checked' checks whether a toggle button has been pressed. With this unsightly way I can turn the snapping on and off.
works in QGIS 3, to toggle snapping on/off use
iface.mapCanvas().snappingUtils().toggleEnabled()
-
To keep the snapping actions in GUI in sync prefer
QgsProject.instance().setSnappingConfig(your_config)
withyour_config
for example defined asyour_config = QgsProject.instance().snappingConfig().setEnabled(True)
persephone– persephone2024年11月28日 11:20:48 +00:00Commented Nov 28, 2024 at 11:20