I would like to load a specific category of the default style of a layer. Layer and style are stored in a Postgres database.

The loadDefaultStyle function doesn't take arguments, so I tried this from Python Console:

styles = layer.listStylesInDatabase()
layer.loadNamedStyle(styles[2][0], QgsMapLayer.Labeling)

I also tried variations with named parameter, but can't get it to work. It should be possible according to docs


Here a standalone solution where we first list styles, then make a dict to match style id to style layer name. Then, we retrieve the qgis qml style as a string. We use it in a QgsMapLayerStyle, load the qml to the layer and then refresh the canvas. Done! (PS: tested before answering ;) )

layer = iface.activeLayer()

styles = layer.listStylesInDatabase()
namedStylesToId = dict(zip(styles[2], styles[1]))
my_style_name = 'mydemo1'
qml_content, _ = layer.getStyleFromDatabase(namedStylesToId.get(my_style_name))
lStyle = QgsMapLayerStyle(qml_content)


Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.