[QGIS-Developer] pyQGIS: how/where to set masked symbol layers and mask sources

Anne B. Nilsen anne.b.nilsen at nibio.no
Wed Dec 14 07:26:59 PST 2022


Hi,
I'm trying the set label masks for heights on a contour line layer.
It works like a charm manually set with Label mask and Masks. I saved the style in a qml-file.
However, when I open/add this style-file in python script, the mask settings (masked symbol layer and mask source) are missing?!? (Labels are checked when saving the qml-file)

Therefore, I would like to add the mask with pyQGIS, but how do I get the name (id) of the "Masked symbol layers" and the "Mask sources"?
Must Masks be set in Layer properties to appear? Ref. https://gis.stackexchange.com/questions/447362/pyqgis-to-set-masked-symbol-layer-and-mask-source
Some of the code I've tried so far:

# Add geopackage
fileName = "karplantehoyder.gpkg"
layer = QgsVectorLayer(fileName, "hoyder", "ogr")
# Load style
layer.loadNamedStyle('./qml/n5hoyder.qml')  # color, line width, label font/size set, but not mask
label = QgsPalLayerSettings()
label.MaskEnabled = True
# masklayer = QgsMaskMarkerSymbolLayer()  # How do i set the mask symbol layer and mask source
# masklayer.setMasks("hoyder")            # This is not working, have tried id and layer - not working
QgsProject.instance().addMapLayer(layer, True)
layer.saveStyleToDatabase(layer.name(),'style n5hoyder',True,'')
mask_settings = QgsTextMaskSettings()
mask_settings.maskedSymbolLayers()      # How do I set the contour line layer to be the Masked symbol layer?
mask_settings.setMaskedSymbolLayers()   # and how do I set the contour layer to be the label mask?


Best regards
Anne Nilsen

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/qgis-developer/attachments/20221214/3d042ea7/attachment.htm>


More information about the QGIS-Developer mailing list