I have a PyQGIS script to open a GPKG-layer (contour lines) along with a qml-file in QGIS. In the style file I have enabled label masking and using the contour lines as mask layer:
Making my labels look like this:
However, after running the python-file, the style is as expected (as given in the qml-file), but without the masking (label masking information is not stored in the qml-file?):
My code so far where I have not figured out how to set "Masked symbol layers" and "Mask sources":
# Add geopackage fileName = "karplantehoyder.gpkg" layer = QgsVectorLayer(fileName, "hoyder", "ogr") 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) # Load style layer.loadNamedStyle('./qml/n5hoyder.qml') # color, line width, label font/size set, but not mask layer.saveStyleToDatabase(layer.name(),'style n5hoyder',True,'')
How may I do this with PyQGIS-code?
Update: I have also tried to use
QgsTextMaskSettings, but I can't figure out how to set the mask layer to be
"hoyder" and symbol to be lines.
I have also discovered a strange behaviour? Masks may be set in the layer hoyder's Layer Properties and still be unchecked in Layer styling, and the mask will appear.
If only set in the Layer Styling and not in the Layer Properties, the mask will not appear.