This question is somewhat involved, so please bear with me. I am updating several plugins from QGIS 2 to 3. Most of them include accessing spatialite databases and creating layers from the various tables in the databases, both with and without geometries. After making the necessary code changes to get the layers to load, I noticed that the General tab under Log Messages included the following warning for each of the layers created.

WARNING Could not load styles from [path] (Query [SELECT statement])

I discovered that saving the default style to the database as follows made the warnings disappear (saving to Local Database did not fix the issue).

Layer==>Properties==>Symbology==>Style==>Save as Default==>Datasource Database

However, this in turn produced another error and prompted this question.


The following code worked until saving the default style:

renderer = segments_layer.renderer()  
root_rule = renderer.rootRule()  # This line fails
rule = root_rule.children()[0].clone()

It seems that saving the styles changed the renderer of the layer from QgsRuleBasedRenderer to QgsSingleSymbolRenderer, and the latter does not have a rootRule() attribute which is required in subsequent lines to function properly.

How can I have the best of both worlds, where I am not getting the WARNING messages and the code works as required?


Strange how asking a question often leads to an answer...

At the top of the Properties==>Symbology page is a combo box that allows for the selection of the rule type. When saving the default rule, select Rule-Based (in this case) from the list and then save the default. Choosing the correct render type ensures that the default rule type is appropriate for the code you need to run.

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.