Hi all, This is the same issue that I had a few months ago [1]. (Thanks Tom for flagging it up).
You can recreate in the console as follows as follows (QGIS 2.14.3, with one vector layer loaded): ``` vlayer = QgsMapLayerRegistry.instance().mapLayers().values()[0] proj = QgsProject.instance() proj.setSnapSettingsForLayer(vlayer.id(), True, QgsSnapper.SnapToVertex, QgsTolerance.LayerUnits, 5.0, False) ``` The settings are not applied. "Settings > Snapping Options > Snapping Mode" remains at "Current Layer". Changing this to to "Advanced" shows correct values have been set, but they are not activated until "Apply" is pressed. Connecting a function to QgsProject.instance().snapSettingsChanged() shows that the signal fires with no arguments when setSnapSettingsForLayer is called. Emitting the signal manually does nothing. Cheers John [1]: http://osgeo-org.1560.x6.nabble.com/Setting-snapping-settings-via-Python-td5246362.html John Stevenson Senior Developer Email:mailto:john.steven...@thinkwhere.com Glendevon House Castle Business Park Stirling FK9 4TZ Tel: 01786 476060 Tel: 01786 476093 (Direct Dial) ________________________________________ From: Qgis-developer [qgis-developer-boun...@lists.osgeo.org] on behalf of Matthias Kuhn [matth...@opengis.ch] Sent: 23 June 2016 14:55 To: qgis-developer@lists.osgeo.org Subject: Re: [Qgis-developer] Problem definig snapping options programmatically Hi Christian, Does it help if you call QgsProject.instance().snapSettingsChanged() Cheers Matthias On 06/23/2016 03:38 PM, EFTAS Christian Röttger wrote: > Hi developers, > > > > i’m facing a problem which i do not understand. In our plugin I created > a function to set snapping options and bound it to a shortcut. > > Using Qgis 2.14.3, source code see below. > > > > The assigned shortcut works well, but for the snapping I have to do the > following on every QGIS start: > > Go to snapping options, mode = advanced, press ok (without changing > anything). Then it works afterwards! > > But when I print the options *before* I do this, I definitely get the > right options (see below). > > > > Does anyone know what I’m missing? Should I open an bug report? Is there > a workaround do do: “Go to snapping options, mode = advanced, press ok” > programmatically ? > > > > Thanks for any help > > Christian > > > > ## Print result > > Initialize, when aggroInstance is true > > (True, False, 2, 0, 2.0, True) > > (True, False, 2, 0, 2.0, True) > > (True, False, 2, 0, 2.0, True) > > (True, False, 2, 0, 2.0, True) > > (True, False, 2, 0, 2.0, True) > > (True, False, 2, 0, 2.0, True) > > (True, False, 2, 0, 2.0, True) > > (True, False, 2, 0, 2.0, True) > > checked > > (True, False, 2, 0, 2.0, True) > > (True, False, 2, 0, 2.0, True) > > (True, True, 2, 0, 2.0, False) > > (True, False, 2, 0, 2.0, True) > > (True, False, 2, 0, 2.0, True) > > (True, True, 2, 0, 2.0, False) > > (True, False, 2, 0, 2.0, True) > > (True, False, 2, 0, 2.0, True) > > > > ## function > > > > *def *toggleSnapping(checked): > /''' > Toggle snapping option > /*:param*/checked: True if snapping has been activated, False otherwise > ''' > /aggroInstance = initModule.aggroInstance > *print **'Initialize' > **if *bool(agroInstance): > *for *item *in *QgsMapLayerRegistry.instance().mapLayers().values(): > QgsProject.instance().setSnapSettingsForLayer(item.id(), > False, 2, 0, 2, True) > fieldLayer = > mapUtils.getLayerByName(aggroInstance.dictData[*'field'*]) > backgroundLayer = mapUtils.getLayerByName(*'bg'*) > *for *item *in *QgsMapLayerRegistry.instance().mapLayers().values(): > *print *QgsProject.instance().snapSettingsForLayer(item.id()) > *if *checked: > *print **'checked' > */# Convenience function to set snap settings per layer. > # it defines the snapping options: > # id : the id of your layer, True : to enable the layer > snapping, 2 : options (2: vertex+segment), 0: type of unit on map, 2 : > tolerance, true : avoidIntersection) > > /QgsProject.instance().setSnapSettingsForLayer(fieldLayer.id(), True, 2, > 0, 2, False) > > QgsProject.instance().setSnapSettingsForLayer(backgroundLayer.id(), > True, 2, 0, 2, False) > *for *item *in > *QgsMapLayerRegistry.instance().mapLayers().values(): > *print > *QgsProject.instance().snapSettingsForLayer(item.id()) > > > > ## initialization in run() method > > > > /# Call Snapping Tool by a key shortcut > /*for *action *in *self.actions: > *if *action.text() == *'Toggle Snapping'*: > /# Action triggered by shortcut key > /self.iface.registerMainWindowAction(action, *"c"*) > > > > > > > > > > > > -- > > image003 > > eftas3 > > Dipl.-Geoinf. Christian Röttger > > -Forschung und Entwicklung > > E F T A S Fernerkundung > Technologietransfer GmbH > Oststraße 2-18 > 48145 Münster > > Fon: +49 251 13307-23 E-Mail: christian.roett...@eftas.com > <mailto:christian.roett...@eftas.com> > Fax: +49 251 13307-33 Web: http://www.eftas.com > <http://www.eftas.com/> > > Geschäftsführer: > Dipl.-Ing. Georg Altrogge > > Sitz der Gesellschaft: Münster > Amtsgericht Münster, HRB 2999 > USt.-IdNr. DE 126038986 > > ****************************************************************** > > image007 > > > > > > _______________________________________________ > Qgis-developer mailing list > Qgis-developer@lists.osgeo.org > List info: http://lists.osgeo.org/mailman/listinfo/qgis-developer > Unsubscribe: http://lists.osgeo.org/mailman/listinfo/qgis-developer > _______________________________________________ Qgis-developer mailing list Qgis-developer@lists.osgeo.org List info: http://lists.osgeo.org/mailman/listinfo/qgis-developer Unsubscribe: http://lists.osgeo.org/mailman/listinfo/qgis-developer _______________________________________________ Qgis-developer mailing list Qgis-developer@lists.osgeo.org List info: http://lists.osgeo.org/mailman/listinfo/qgis-developer Unsubscribe: http://lists.osgeo.org/mailman/listinfo/qgis-developer