I have some strange behavior regarding the snapping options function setSnapSettingsForLayer in the QgsProject class.

I am setting the snapping parameters in the following way:

QgsProject.instance().setSnapSettingsForLayer(myLayer.id(), True, QgsSnapper.SnapToVertexAndSegment, QgsTolerance.Pixels, 10, False)

The snapping does not seem activated (there is no pink cross on my line layer that I called). When I open the snapping options via the GUI, what I first see is the snapping mode combo box on "Current Layer" with the snapping turned off. When switching to Advanced snapping, the layer I called in the method does indeed have snapping turned on with the correct parameters. Now, when clicking okay, the snapping is activated.

Any input on how I can get the snapping options turned on without having to navigate through the GUI?

Some more information, in case it has to do with the way the layer has been referenced--the layer is defined in this way:

layers = iface.mapCanvas().layers()
for layer in layers:
    if layer.name() == 'MyLayer':
        myLayer = layer

I am now testing it this way...but it doesn't seem to make a difference.

layers = QgsMapLayerRegistry.instance().mapLayers().values()
for layer in layers:
    if layer.name() == 'MyLayer':
        myLayer = layer

What I think is the problem:

I believe it has to do with the way the Current Layer snapping mode is set up. My code works okay when the project is saved with this mode on "sur un sommet et un segment" (endpoints and segments, in english, I believe). When the project has been saved with this mode on "Off" however, the advanced settings (set programmatically) do not override these settings. Is this correct? If yes, is there any way around it? Such as programmatically setting the "Current Layer" mode as well to ensure that it will be on the correct setting.

I found this QgsSnappingUtils::SnapToMapMode in the API, and am trying it this way to see if it gets me where I need (forcing the project to use snapping options based on the advanced mode, and not on current layer):


I am getting a TypeError though:

TypeError:QgsSnappingUtils.setSnapToMapMode(QgsSnappingUtils.SnapToMapMode): first argument of unbound method must have type 'QgsSnappingUtils'

  • 1
    +1 same problem with 2.14. I don't know how to set Snapping mode to "Advanced". QgsSnappingUtils().setSnapToMapMode(2) does not work...
    – WKT
    Commented Apr 15, 2016 at 21:35
  • Using QgsSnappingUtils.setSnapToMapMode(QgsSnappingUtils.SnapAdvanced) does not raise error (QGIS 2.14) but have no effect... Snapping mode stay "Current layer". setSnapSettingsForLayer works fine, like you...
    – WKT
    Commented Apr 16, 2016 at 9:18
  • Some news ? workaround ?
    – WKT
    Commented May 11, 2016 at 8:53
  • @WKT not yet--my workaround for the moment includes saving the project with the proper parameters...not really a solution. Do let me know if you have any leads.
    – user25976
    Commented May 17, 2016 at 20:43
  • Trying new question: gis.stackexchange.com/questions/195760/…
    – WKT
    Commented Jun 3, 2016 at 9:28

1 Answer 1


same question, same answer:

I find a solution to that. As it seems to be impossible to access snapping mode from the QgsProject, I try to access it from the QgsInterface. This works for me in python Console:

from PyQt4.QtGui import QDockWidget, QDialog,QComboBox

iface.mainWindow().findChild(QDockWidget, 'Snapping and Digitizing Options').findChild(QDialog).findChild(QComboBox,'mSnapModeComboBox').setCurrentIndex(0) 

Change the current Index to fit to your needs: 0 = current layer 1 = all layers 2 = advanced

then you can set your snapping's settings with QgsProject.instance().setSnapSettingsForLayer(layer.id(),...)

Not the answer you're looking for? Browse other questions tagged or ask your own question.