2

I tried

>>>QgsVectorLayer.loadNamedStyle(r'C:\Some\Path\FOO.qlr')

Get this error:

Traceback (most recent call last):
  File "<input>", line 1, in <module>
TypeError: arguments did not match any overloaded call:
  QgsVectorLayer.loadNamedStyle(QString, bool) -> (QString, bool): first argument of unbound method must have type 'QgsVectorLayer'
  QgsVectorLayer.loadNamedStyle(QString) -> (QString, bool): first argument of unbound method must have type 'QgsVectorLayer'

Then i tried:

>>>vlayer = QgsVectorLayer
>>>vlayer = loadNamedStyle(r'C:\Some\Path\FOO.qlr')

and gets the same error. The question is simple. How do i load a .qlr-file from path?

My research: The developers archive, The C++ documentation (which i suppose is good but I don't know C++), Some one with a similar problem

I have a hard time finding Api for the py qgis.

4

QGIS 2.x

The source file for layer definitions indicates that you need to use loadLayerDefinition and not loadNamedStyle. Therefore we need to specify a string path and a QgsLayerTreeGroup in which the layer will be loaded into. Then we can either:

  • Create a new empty group in the Table of Contents:

    root = QgsProject.instance().layerTreeRoot()
    group = root.addGroup("group1")
    QgsLayerDefinition().loadLayerDefinition('C:/Some/Path/FOO.qlr', group)
    
  • Or use an existing group:

    root = QgsProject.instance().layerTreeRoot()
    group = root.findGroup('group1')
    QgsLayerDefinition().loadLayerDefinition('C:/Some/Path/FOO.qlr', group)
    

QGIS 3.x

The QgsLayerDefinition().loadLayerDefinition() function now requires the project instance as an input parameter. So the last line of each of the above code snippets can be replaced from:

QgsLayerDefinition().loadLayerDefinition('C:/Some/Path/FOO.qlr', group)

to:

QgsLayerDefinition().loadLayerDefinition('C:/Some/Path/FOO.qlr', QgsProject.instance(), group)
  • Yes I have loaded the .qlr file from path. – VilyaSeeFour May 17 '17 at 11:18
  • How do I draw the group? Nothing shows upp in the map. – VilyaSeeFour May 17 '17 at 11:20
  • 1
    @GeorgeC - Thanks for your comment, I've edited the post. You just need to add the project instance as a secondary parameter. I.e. QgsLayerDefinition().loadLayerDefinition('C:/Some/Path/FOO.qlr', QgsProject.instance(), group) – Joseph Mar 6 at 10:33
  • 1
    @GeorgeC - I would suggest you ask this as a new question and include as much detail as possible including what you saved in your .qlr file and the type of layer you are applying it to. – Joseph Mar 7 at 10:20
  • 1
    @Joseph see gis.stackexchange.com/questions/314713/… – GeorgeC Mar 7 at 10:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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