I am saving my layer-styles in the same spatialite-database, where the layer-data is saved and I am able to load the layer (with the default style) to the canvas (using PyQGIS in a plugin).

But how do I load an alternative style for this layer from my spatialite-database (from the table 'layer_styles')?

I have found the method loadNamedStyleFromDatabase(self, db: str, uri: str), but I have troubles to set the parameters. I think db is like G:/path/to/database.db. But how to set the parameter uri correct to load an alternative layer-style.

Or is there an other way to do this. E.g. I can get the qml- or sld-definition from the table 'layer_styles' for the wanted layer. But how to load the layer with this definitions?

EDIT on 16.05.2019:

I have now tried to load the style with importNamedStyle like this:

layer = QgsVectorLayer(uri, base_name, provider)

style_qml = layer.getStyleFromDatabase(str(style_id))
style_doc = QDomDocument("qgis")


I get the values for uri, base_name, provider and style_id from the database and I have controlled them, they are right (incl. style_qml).

I have e.g. two styles for one layer stored in the database. On running the code above, then only one of them is loading. No one of the two is set as the default style.

How can I load the style correct?

  • You can only load one style at the time. You can manually merge the two styles in QGIS and resave it to the DB. – Jakob Jan 7 at 10:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.