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)

  • @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 Jun 2 '16 at 9:36
  • Related: QgsSnappingUtils().snapToMapMode() always returns 0 even if I change snap mode with the GUI... – WKT Jun 3 '16 at 9:26

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


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)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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