4

I am trying to apply the style from .qml to a group of layers in QGIS. I have tried to incorporate LoadNamedStyle within jkall's code here: https://github.com/jkall/qgis-processing-scripts/blob/master/set_transparency_for_group.py I end up with this:

from qgis.core import *
def applystyle_group(name):
    root = QgsProject.instance().layerTreeRoot()
    point = root.findGroup(name) #Find Group AP
    for child in point.children():
        if isinstance(child, QgsLayerTreeLayer):
            if child.layer().type()==0:
                child.loadNamedStyle('*.qml')
applystyle_group("AK")

It gives me an error:

Traceback (most recent call last):
  File "<input>", line 1, in <module>
  File "C:/OSGeo4W64/bin/applystyle_group.py", line 9, in <module>
     applystyle_group("AK")
  File "C:/OSGeo4W64/bin/applystyle_group.py", line 8, in applystyle_group
    child.loadNamedStyle('*.qml')
AttributeError: 'QgsLayerTreeLayer' object has no attribute 'loadNamedStyle'

Does QgsLayerTreeLayer have something similar to loadNamedStyle?

1 Answer 1

6

Change this line

child.loadNamedStyle('*.qml')

by:

child.layer().loadNamedStyle('/some/real/path/to/a/file.qml')

Your child object is of type QgsLayerTreeLayer, but you need to access layer() to get its correspondent QgsMapLayer, which has a loadNamedStyle() method.

4
  • Wonderful German, You saved me again!
    – B-C B.
    Jul 20, 2017 at 19:36
  • A-ha, don't tell me you are BC14W! :) Jul 20, 2017 at 20:42
  • Haha! yes, I am! :)
    – B-C B.
    Jul 20, 2017 at 21:18
  • 1
    Marked it as accepted. I don't have 15 reputations yet, so I can't up-vote yet.
    – B-C B.
    Jul 20, 2017 at 21:45

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.