Tuesday, 9 June 2015

qgis plugins - Set Snap Mode with PyQGIS


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)



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



No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...