2

Using the toolbar GUI of QGIS (Currenly using v3.28), I can enabled multiple/all the snapping types like this:

Snapping Modes Enabled

Now that's exactly what I'm trying to do in this code: (Just setting 2 types for the example)

snapping_config = QgsSnappingConfig() 
snapping_config.setEnabled(True)
snapping_config.setMode(QgsSnappingConfig.AllLayers)

# This only enables Area Snapping
snapping_config.setTypeFlag(QgsSnappingConfig.AreaFlag)

# Using the same method again with different type, only overwrites the previously set type.
snapping_config.setTypeFlag(QgsSnappingConfig.CentroidFlag)

QgsProject.instance().setSnappingConfig(snapping_config)

But I can't figure it out since the setTypeFlag method only accepts only one SnappingType enum object.

https://github.com/qgis/QGIS/blob/release-3_38/src/core/qgssnappingconfig.h#L255

How can I do that in QGIS python?

1 Answer 1

6

You can set multiple Types using Qgis.SnappingTypes:

snapping_config.setTypeFlag(Qgis.SnappingTypes(Qgis.SnappingType.Vertex | Qgis.SnappingType.CentroidFlag | Qgis.SnappingType.Area))

You can find all SnappingType Enums in the documentation:

https://qgis.org/pyqgis/3.38/core/Qgis.html#qgis.core.Qgis.SnappingType

1
  • That works! I can't seem to figure out how you found out the SnappingType.Vertex enum, I was using it as with the Flag word: SnappingType.VertexFlag
    – Imaginary
    Commented Aug 26 at 7:11

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.