<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
</head>
<body>
<font size="+1">I'm writing a Python script to load a "qml" style
file. The following two lines work correctly: <br>
lyr1 = iface.activeLayer()<br>
lyr1.loadNamedStyle('xxx.qml')<br>
but the following two lines cause an error message:<br>
lyr2 = instance.parameterAsSource(parameters, "INPUT", context)
<br>
lyr2.loadNamedStyle('xxx.qml')<br>
AttributeError: QgsProcessingFeatureSource: object has no
attribute 'loadNamedStyle'<br>
My problem is that <br>
lyr1 is a QgsVectorLayer but lyr2 is a
QgsProcessingFeatureSource<br>
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<br>
======================<br>
from qgis import processing<br>
from qgis.processing import alg<br>
from qgis.core import QgsProject<br>
from qgis.utils import iface<br>
<br>
@alg(name='getMyStyle', label='load BigPicStyle on the active
layer (alg)',<br>
group='examplescripts', group_label='Example scripts')<br>
@alg.input(type=alg.SOURCE, name='INPUT', label='Input vector
layer')<br>
@alg.input(type=alg.STRING, name='theStyle', label='name of style
file')<br>
@alg.output(type=alg.STRING, name='OUTPUT', label='output lbl')<br>
<br>
def getMyStyle(instance, parameters, context, feedback, inputs):<br>
"""<br>
Asks for a style name and then loads that style. By default,<br>
the layer acted on is the active layer, but a dropdown<br>
menu lets user choose another layer.<br>
"""<br>
stylePath = 'C:/Promenade/GIS/data/styles/'<br>
whichFile = parameters.get('theStyle')<br>
# note: lyr1 is QgsVectorLayer; lyr2 is
QgsProcessingFeatureSource<br>
lyr1 = iface.activeLayer()<br>
lyr2 = instance.parameterAsSource(parameters, "INPUT",
context)<br>
lyr1.loadNamedStyle(stylePath + whichFile + '.qml') #this
works <br>
lyr1.triggerRepaint()<br>
#next line causes AttributeError:<br>
# 'QgsProcessingFeatureSource' object has no attribute
'loadNamedStyle'<br>
lyr2.loadNamedStyle(stylePath + whichFile + '.qml')<br>
lyr2.triggerRepaint()<br>
</font>
</body>
</html>