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':
iface.setActiveLayer(layer)
iface.actionDuplicateLayer().trigger()
# 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
iface.legendInterface().refreshLayerSymbology(vl)
#Refresh canvas
canvas = iface.mapCanvas()
canvas.refresh()
QgsRuleBasedRendererV2.convertFromRenderer(renderer)