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

Julien Cabieces julien.cabieces at oslandia.com
Wed Apr 26 09:01:41 PDT 2023


Hi,

I changed the way we referenced symbol layer to make it more simple and
robust. Now, symbol layer have a generated unique id so it's easier to reference
each of them (instead of a path of indices in the symbol layers tree before).

I see 2 solutions here:

1. You keep your old references and call
QgsProjectFileTransform.fixOldSymbolLayerReferences() on your layers

2. You change the way you create the references. here[0] is an example on
how you can create QgsSymbolLayerReference. 

I would favor solution 2 because you wouldn't rely on deprecated method
and so you would be more future proof for when we eventually drop these
methods.

Regards,
Julien

[0] https://github.com/qgis/QGIS/pull/50786/files#diff-c7f779c945b0e9c401ddb964691ec0d6a07c6c3fcfca0e3db146f97aec2d17e2R177

> 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
>
> _______________________________________________
> QGIS-User mailing list
> QGIS-User at lists.osgeo.org
> List info: https://lists.osgeo.org/mailman/listinfo/qgis-user
> Unsubscribe: https://lists.osgeo.org/mailman/listinfo/qgis-user


-- 

Julien Cabieces
Senior Developer at Oslandia
julien.cabieces at oslandia.com
09.72.52.52.76


More information about the QGIS-User mailing list