Skip to content

Instantly share code, notes, and snippets.

@NathanW2
Forked from mtravis/snapping.py
Last active October 17, 2015 11:54
Show Gist options
  • Save NathanW2/77833fae5444568337c0 to your computer and use it in GitHub Desktop.
Save NathanW2/77833fae5444568337c0 to your computer and use it in GitHub Desktop.
turn snapping on/off in QGIS
from qgis.utils import iface
from PyQt4.QtCore import *
from PyQt4.QtGui import *
def snapping_toggle():
#layer = QgsMapLayerRegistry.instance().mapLayersByName("topographicline")[0]
layer = iface.activeLayer()
_, enabled, _, _, _, _ = QgsProject.instance().snapSettingsForLayer(layer.id())
QgsProject.instance().setSnapSettingsForLayer(layer.id(), not enabled, 0, 1, 10, True)
print "set {} snapping {}".format(layer.name(), "Off" if not enabled else "On")
shortcut = QShortcut(QKeySequence(Qt.SHIFT + Qt.Key_S), iface.mainWindow())
shortcut.setContext(Qt.ApplicationShortcut)
shortcut.activated.connect(snapping_toggle)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment