I want to make a copy from an existing layer in the legend and render it with a rule-based renderer based on the original layer's graduated symbol renderer. Then I will change its rule expressions, clone all the rules (if that is even possible) and change again their expressions.

Right now I'm at step one: creating a rule-based renderer from an existing graduated symbol renderer. With iface.actionDuplicateLayer().trigger() I clone the original layer with its renderer. Then I use convertFromRenderer function to create a QgsRuleBasedRendererV2 from the new layer's graduated symbol renderer and I assign it to the layer. But there's something I'm doing wrong because QGIS crashes at this point and I can't find the reason.

# Duplicate original layer
for layer in iface.legendInterface().layers():
    if layer.name() == 'Lines':
# Make the new layer visible and change its name
for layer in iface.legendInterface().layers():
    if layer.name() == 'Lines copy':
        vl = layer
iface.legendInterface().setLayerVisible(vl, True)
vl.setLayerName('Lines - Atlas')

# Create rule-based renderer from its graduated symbol renderer
#  and assign it to it
renderer  = vl.rendererV2()
if renderer.type() == 'graduatedSymbol':
    rSimbol = renderer.sourceSymbol()
    ruleRend = QgsRuleBasedRendererV2(rSimbol).convertFromRenderer(renderer)
    vl.setRendererV2(ruleRend) #<------- QGIS crashes here

#Refresh canvas
canvas = iface.mapCanvas()
  • 1
    Does that work better? QgsRuleBasedRendererV2.convertFromRenderer(renderer) Oct 27, 2016 at 9:50
  • Thank you so much @MatthiasKuhn. That has sense. There is no need to provide a default symbol to the renderer since it will take everything from the original renderer. I don't know why I discarded that possibility.
    – DMS02
    Oct 27, 2016 at 10:24
  • Great, turned it into an answer ;) Oct 27, 2016 at 10:25

1 Answer 1


convertFromRenderer is a static function and therefore does not need an instance to work on.

You can simply use the following:

if renderer.type() == 'graduatedSymbol':
    ruleRend = QgsRuleBasedRendererV2.convertFromRenderer(renderer)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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