7

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

enter image description here

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?

asked Jun 3, 2016 at 9:13
3
  • What version of QGIS are you using? Commented Jun 3, 2016 at 12:43
  • Other related problem: gis.stackexchange.com/questions/195760/… Commented Jun 3, 2016 at 13:11
  • @artwork21 Version 2.14.0 Essen Commented Jun 7, 2016 at 6:10

2 Answers 2

4

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.

Germán Carrillo
37.3k5 gold badges127 silver badges182 bronze badges
answered Jun 6, 2016 at 14:11
2

works in QGIS 3, to toggle snapping on/off use

iface.mapCanvas().snappingUtils().toggleEnabled()

https://qgis.org/api/classQgsSnappingUtils.html

answered Mar 6, 2018 at 16:30
1
  • To keep the snapping actions in GUI in sync prefer QgsProject.instance().setSnappingConfig(your_config) with your_config for example defined as your_config = QgsProject.instance().snappingConfig().setEnabled(True) Commented Nov 28, 2024 at 11:20

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.