[Qgis-user] PyQGIS to set masked symbol layer and mask source in ver. 3.30

Anne B. Nilsen anne.b.nilsen at nibio.no
Wed Apr 26 04:52:29 PDT 2023


Thanks to the answer to this post https://gis.stackexchange.com/questions/447362/pyqgis-to-set-masked-symbol-layer-and-mask-source
I managed to set the mask layer in QGIS version 3.28.

The code is not working In ver. 3.30 due to a change in the QgsSymbolLayerReference class.
However I'm not able to change the original code so it fits with the new specification

I just don't understand how to set the input parameters to fulfil the requirements in https://qgis.org/pyqgis/3.30/core/QgsSymbolLayerReference.html#qgis.core.QgsSymbolLayerReference

# Set masked symbol layers :  setMaskedSymbolLayers(self, maskedLayers: Iterable[QgsSymbolLayerReference])

fileName = "./gpkg/karplantehoyder.gpkg"
layer = QgsVectorLayer(fileName, "hoyder", "ogr")
layer.loadNamedStyle('./qml/n5hoyder.qml')      # Load qml
QgsProject.instance().addMapLayer(layer, False) # Add layer False to be able
root.insertLayer(4,layer)                       # to specify a custom position
label_settings = layer.labeling().settings()    # Get current settings of the layer
text_format = label_settings.format()           # Get text format of the settings

# old code (working in 3.28)
# text_format.mask().setMaskedSymbolLayers([QgsSymbolLayerReference(layer.id(), QgsSymbolLayerId("", 0))])  # set masked symbol layers *****

# -- New code
linesymbol = QgsLineSymbol()
lyr = linesymbol.symbolLayer(0)                 # Trying to get the id of the line layer (hoyder)
text_format.mask().setMaskedSymbolLayers([layer.id(), lyr.id()])  # set masked symbol layers - not working
# --

label_settings.setFormat(text_format)           # set format
layer.labeling().setSettings(label_settings)    # set settings
layer.saveStyleToDatabase(layer.name(),'style n5hoyder',True,'')  # Save style in gpkg

Anyone who knows how I should write the code marked with *****?

Kind regards
Anne
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/qgis-user/attachments/20230426/4fd40506/attachment.htm>


More information about the QGIS-User mailing list