1

I'm trying to change the width of a categorized line layer with PyQGIS.

Here is my code :

NOX_Actuel=iface.activeLayer()
target_field = 'NOX_Actuel'
myRangeList = []
symbol = QgsSymbol.defaultSymbol(NOX_Actuel.geometryType())
symbol.setColor(QColor("#67000d"))
myRange = QgsRendererRange(15.0, 500.0, symbol, '> 15')
myRangeList.append(myRange)
myRenderer = QgsGraduatedSymbolRenderer(target_field, myRangeList)
symbol.setWidth(5)
myRenderer.setMode(QgsGraduatedSymbolRenderer.Custom)
NOX_Actuel.setRenderer(myRenderer)
NOX_Actuel.triggerRepaint()

Whatever I try, I've the following error : "AttributeError: 'QgsGraduatedSymbolRenderer' object has no attribute 'setWidth'"

1

You probably select a non-line layer before running the script. You should select a line layer. QgsSymbol.defaultSymbol() returns default symbol (QgsMarkerSymbol, QgsFillSymbol or QgsLineSymbol) for related geometry type.

The reason you get the error is that just QgsLineSymbol has setWidth() method, but QgsMarkerSymbol for points and QgsFillSymbol for polygons don't have.

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.