Re: [Qgis-user] loadNamedStyle

2021-09-14 Thread Etienne Trimaille
Quick answer,

You might want to have a look to
https://qgis.org/api/classQgsProcessingUtils.html#aa1c2360e52d2ed8100faf02a35125b17
I'm not sure about writing a processing script with this syntax, I'm still
using the class way. But you might want to set your style in a post
processor QgsProcessingLayerPostProcessorInterface?

Or why not using the builtin processing script to load a style ? Just
pre-fill values and call this algorithm ?
___
Qgis-user mailing list
Qgis-user@lists.osgeo.org
List info: https://lists.osgeo.org/mailman/listinfo/qgis-user
Unsubscribe: https://lists.osgeo.org/mailman/listinfo/qgis-user


[Qgis-user] loadNamedStyle

2021-09-13 Thread Stephen Sacks
I'm writing a Python script to load a "qml" style file.  The following 
two lines work correctly:

   lyr1 = iface.activeLayer()
   lyr1.loadNamedStyle('xxx.qml')
but the following two lines cause an error message:
   lyr2 = instance.parameterAsSource(parameters, "INPUT", context)
   lyr2.loadNamedStyle('xxx.qml')
   AttributeError:  QgsProcessingFeatureSource: object has no attribute 
'loadNamedStyle'

    My problem is that
    lyr1 is  a  QgsVectorLayer  but  lyr2 is a 
QgsProcessingFeatureSource
So the issue comes down to how to load a named style file onto a 
QgsProcessingFeatureSource.   The complete code is below.   Thanks for 
any advice.   Steve

==
from qgis import processing
from qgis.processing import alg
from qgis.core import QgsProject
from qgis.utils import iface

@alg(name='getMyStyle', label='load BigPicStyle on the active layer (alg)',
 group='examplescripts', group_label='Example scripts')
@alg.input(type=alg.SOURCE, name='INPUT', label='Input vector layer')
@alg.input(type=alg.STRING, name='theStyle', label='name of style file')
@alg.output(type=alg.STRING, name='OUTPUT', label='output lbl')

def getMyStyle(instance, parameters, context, feedback, inputs):
    """
    Asks for a style name and then loads that style.  By default,
   the layer acted on is the active layer, but a dropdown
   menu lets user choose another layer.
    """
    stylePath = 'C:/Promenade/GIS/data/styles/'
    whichFile = parameters.get('theStyle')
    #  note: lyr1 is QgsVectorLayer;  lyr2 is QgsProcessingFeatureSource
    lyr1 = iface.activeLayer()
    lyr2 = instance.parameterAsSource(parameters, "INPUT", context)
    lyr1.loadNamedStyle(stylePath + whichFile + '.qml') #this works
    lyr1.triggerRepaint()
    #next line causes AttributeError:
    # 'QgsProcessingFeatureSource' object has no attribute 'loadNamedStyle'
    lyr2.loadNamedStyle(stylePath + whichFile + '.qml')
    lyr2.triggerRepaint()
___
Qgis-user mailing list
Qgis-user@lists.osgeo.org
List info: https://lists.osgeo.org/mailman/listinfo/qgis-user
Unsubscribe: https://lists.osgeo.org/mailman/listinfo/qgis-user