I am trying to set snap mode within QGIS plugin.

So far, I have tried:




without success.
QGIS Snapping options (Settings>>Snapping Options...) continue with: Snapping mode = Current layer

Any hint? (Using v 2.14.0)

  • Not sure if this post might help? How to enable the snapping for a layer with the tolerance value with python programming
    – Joseph
    Commented Jun 2, 2016 at 9:10
  • @Joseph No, it does not help... setSnapSettingsForLayer() works fine and I can define snapping tolerance for each layer without problem. I can't "jump" to Advanced mode with python (setSnapToMapMode).
    – WKT
    Commented Jun 2, 2016 at 9:36
  • Related: QgsSnappingUtils().snapToMapMode() always returns 0 even if I change snap mode with the GUI...
    – WKT
    Commented Jun 3, 2016 at 9:26

2 Answers 2


I fund the what you shearch here: QGIS doesn't update some snapping options from Python

proj = QgsProject.instance()
proj.writeEntry('Digitizing', 'SnappingMode', 'advanced') 
layer = qgis.utils.iface.activeLayer()
proj.setSnapSettingsForLayer(layer.id(), True, 0, 1, 30, True)

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(),...)

Your Answer

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

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