2

I am working with QGIS 3.4

Is there a way to easily copy the styles from one group of layers to another group of layers with the same names? The outcome should be that layers that share the same name, but which are in different groups, end up with the same style.

1 Answer 1

7

Here is a small Python script which should do what you want. Don't worry if you're not familiar with the Python console, just open it with ctrl+alt+P or click the enter image description here button in the plugins toolbar.

Open a new editor with the notepad icon, paste in the code, change the group names to match yours and click the green arrow to run the code. (see buttons highlighted in image below).

enter image description here

source_group_name = 'Your source group name' #Change to match your group
dest_group_name = 'Your destination group name' #Change to match your group

project = QgsProject().instance()
source_group = project.layerTreeRoot().findGroup(source_group_name)
dest_group = project.layerTreeRoot().findGroup(dest_group_name)
for ltl in source_group.findLayers():
    source_style = QgsMapLayerStyle()
    source_style.readFromLayer(ltl.layer())
    dest_layer = [l.layer() for l in dest_group.findLayers() if l.layer().name()==ltl.layer().name()]
    if dest_layer:
        source_style.writeToLayer(dest_layer[0])
        dest_layer.triggerRepaint()

Your Answer

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

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