1

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 alternativ 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 alternativ 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")
style_doc.setContent(str(style_qml))
layer.importNamedStyle(style_doc)

QgsProject.instance().addMapLayer(layer)
self.canvas.refresh()

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?

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.