3

I am trying to set snap mode within QGIS plugin.

So far, I have tried:

QgsSnappingUtils().setSnapToMapMode(QgsSnappingUtils.SnapAdvanced)

and

QgsSnappingUtils().setSnapToMapMode(2)

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

Any hint? (Using v 2.14.0)

3
  • Not sure if this post might help? How to enable the snapping for a layer with the tolerance value with python programming
    – Joseph
    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
    Jun 2, 2016 at 9:36
  • Related: QgsSnappingUtils().snapToMapMode() always returns 0 even if I change snap mode with the GUI...
    – WKT
    Jun 3, 2016 at 9:26

2 Answers 2

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)
1

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, privacy policy and cookie policy

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