1

Based on Load .qlr with loadNamedStyle I was able to add a QLR to a python tool via PyQGIS -however, it appears broken...

            elif ftype=='qlr':
                try:
                    print ('Loading QLR layer')
                    #ref:https://gis.stackexchange.com/questions/240691/load-qlr-with-loadnamedstyle/240694?noredirect=1#comment511181_240694
                    print("QLR %s called and loading into group %s" % (filePath, fname))
                    root=QgsProject.instance().layerTreeRoot()
                    print('adding group')
                    group=root.addGroup(fname)
                    QgsLayerDefinition().loadLayerDefinition(filePath,QgsProject.instance(),group)
                except:
                    print ('Error loading QLR')

fname is the name of the group and file, filepath is the path/file.qlr

The QLR is loading a shp that's in the same folder.

It looks like

 <!DOCTYPE qgis-layer-definition>
<qlr>
  <layer-tree-group checked="Qt::Checked" name="" expanded="1">
    <customproperties/>
    <layer-tree-layer id="events_6d4a5418_1dce_4ba0_201903071250415994831" checked="Qt::Checked" name="events" source="./events.shp" providerKey="ogr" expanded="1">
      <customproperties/>
    </layer-tree-layer>
  </layer-tree-group>
  <maplayers>
    <maplayer autoRefreshEnabled="0" labelsEnabled="1" refreshOnNotifyMessage="" maxScale="0" minScale="1e+08" hasScaleBasedVisibilityFlag="0" readOnly="0" styleCategories="AllStyleCategories" autoRefreshTime="0" type="vector" simplifyDrawingTol="1" refreshOnNotifyEnabled="0" simplifyDrawingHints="0" simplifyAlgorithm="0" simplifyLocal="1" simplifyMaxScale="1" geometry="Point">
      <extent>
        <xmin>142.5193149000000119</xmin>
        <ymin>-37.29052469999999886</ymin>
        <xmax>143.2661899999999946</xmax>
        <ymax>-36.60563100000000247</ymax>
      </extent>
      <id>events_6d4a5418_1dce_4ba0_201903071250415994831</id>
      <datasource>./events.shp</datasource>
      <keywordList>
        <value></value>
      </keywordList>
      <layername>events</layername>

and it comes out like enter image description here

However...

It works fine if I load it in the QGIS python console using the same commands... enter image description here

Note screen grabs are of a complex and simple QLR I tried but both end up the same...

Your Answer

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

Browse other questions tagged or ask your own question.