I have a vector polygon and vector polyline layer. I want to enable the snapping for both the layer with tolerance value? How can i do that using python programming. I have tried this code but getting the following error:

snapper = QgsMapCanvasSnapper(canvas) 

Traceback (most recent call last): File "", line 1, in AttributeError: 'QgsMapCanvasSnapper' object has no attribute 'setSnapSettingsForLayer'

Can anyone help me in setting the snapping options to the currentLayer using python programming?

2 Answers 2


You must define it first:

result3 = QgsVectorLayer("LineString", "ligne", "memory") 
#it's to create your layer

# it allows you to have the idvalue for your layer

# it defines the snapping options ligneid : the id of your layer, True : to enable the layer snapping, 2 : options (0: on vertex, 1 on segment, 2: vertex+segment), 1: pixel (0: type of unit on map), 1000 : tolerance, true : avoidIntersection)

I'm using QGIS 3.22.16. I created a new QgsSnappingConfig() and used setIndividualLayerSettings to add each Layer to my selection. Then I assign the config to the project instance.

snapping_config = QgsSnappingConfig() 
snapping_config.setIndividualLayerSettings(self.gebauedelayer, QgsSnappingConfig.IndividualLayerSettings(True, QgsSnappingConfig.VertexFlag, 10, QgsTolerance.Pixels))
snapping_config.setIndividualLayerSettings(self.flurlayer, QgsSnappingConfig.IndividualLayerSettings(True, QgsSnappingConfig.VertexFlag, 10, QgsTolerance.Pixels))

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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