3

I'm trying to copy symbol of a certain polygon layer to multiple layers (also polygon) in project at once with a condition in cycle. But as soon as the script start to modify a symbol of the second layer, QGIS crashes.

layer = iface.activeLayer()
copyrend = layer.renderer() 


for layer in QgsProject.instance().mapLayers().values():
    if layer.name().find("some_condition_in_name") != -1:
        layer.renderer().setSymbol(copyrend.symbol())   
        layer.triggerRepaint()  

Doing it without a cycle for a single layer works fine, but doing it for the whole project (~1000 layers) concludes in a crash. What can I do to avoid this?

If I don't use a ".triggerRepaint()" for every layer, QGIS will not crash until I'm trying to do something with a layers that has a changed symbol.

0

1 Answer 1

3

You need to set a clone of the symbol for each renderer. Otherwise, it is the same object being assigned to all renderers which causes issues.

layer = iface.activeLayer()
copyrend = layer.renderer() 


for layer in QgsProject.instance().mapLayers().values():
    if layer.name().find("some_condition_in_name") != -1:
        layer.renderer().setSymbol(copyrend.symbol().clone())   
        layer.triggerRepaint()  

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.