[Qgis-developer] Problem definig snapping options programmatically

Matthias Kuhn matthias at opengis.ch
Thu Jun 23 06:55:33 PDT 2016


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.roettger at eftas.com
> <mailto:christian.roettger at 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 at lists.osgeo.org
> List info: http://lists.osgeo.org/mailman/listinfo/qgis-developer
> Unsubscribe: http://lists.osgeo.org/mailman/listinfo/qgis-developer
> 


More information about the Qgis-developer mailing list