[QGIS-Developer] loadNamedStyle() is loading default style instead of QML file

Jacky Volpes jacky.volpes at oslandia.com
Mon Sep 11 05:47:18 PDT 2023


Hi Alexandre,

When the layer is a PostgreSQL layer, it's the method from 
QgsVectorLayer which is called:

https://qgis.org/pyqgis/3.32/core/QgsVectorLayer.html#qgis.core.QgsVectorLayer.loadNamedStyle

https://api.qgis.org/api/classQgsVectorLayer.html#a2bd3a21badac411179dbfbc724136a45


The documentation is somewhat incomplete, as "loadFromLocalDb" boolean 
set to true will in fact call QgsMapLayer::loadNamedStyle()

https://api.qgis.org/api/classQgsMapLayer.html#a8b271b8bf3074e84808cff26f722b26f

which says: "[...] First an attempt will be made to see if this is a 
file and load that, if that fails the qgis.db styles table will be 
consulted to see if there is a style who's key matches the URI."

Regards,

--

Jacky Volpes

Ingénieur SIG - Oslandia

--

Le 9/11/23 à 13:33, Alexandre Neto via QGIS-Developer a écrit :
> I am trying to load a style from a QML file into a vector layer.
>
> When I use a memory layer, loading the style works fine with the 
> following code:
>
> layer = iface.activeLayer()
> layer.loadNamedStyle(path_to_style)
>
> If the layer is a postgreSQL layer with a default style, the above 
> code will return:
>
> ('Loaded from provider', True)
>
> and the style from the QML won't be loaded. The way to force it was 
> add a True parameter in the end of the method call:
>
> layer = iface.activeLayer()
> layer.loadNamedStyle(path_to_style, True)
>
> Not sure if this is the correct behavior, but it does not match with 
> the documentation:
>
> https://api.qgis.org/api/classQgsMapLayer.html#a8b271b8bf3074e84808cff26f722b26f
>
> Thanks,
>
> Alexandre Neto
>
> _______________________________________________
> QGIS-Developer mailing list
> QGIS-Developer at lists.osgeo.org
> List info: https://lists.osgeo.org/mailman/listinfo/qgis-developer
> Unsubscribe: https://lists.osgeo.org/mailman/listinfo/qgis-developer


More information about the QGIS-Developer mailing list