-1

In QGIS 3.22 I'm facing a problem with QgsMapLayer. I want to use it on QGIS in order to modify the symbology of line, point & polygon in each layer but every time it gives me an error on using the library of PyQGIS.

This is the script I'm using:

from PyQt5.QtGui import *
from qgis.core import * 

lyr = QgsVectorLayer("PATH", 'ligne', 'ogr')
QgsProject.instance().addMapLayer(lyr)
sym = QgsLineSymbol.createSimple({'Line_style': 'dash', 'Color': 'red'})
symbol = lyr.QgsMapLayer.createMapRenderer().symbols(QgsRenderContext())
sym = symbols[0]
lyr.triggerRepaint()

And here is the error message:

AttributeError: 'QgsVectorLayer' object has no attribute 'QgsMapLayer'

Is there is any solution for that?

1

1 Answer 1

4

I don't fully understand what you are trying to do but try this:

lyr = QgsVectorLayer("PATH", 'ligne', 'ogr')
QgsProject.instance().addMapLayer(lyr)

sym = QgsLineSymbol.createSimple({'line_style':'dash', 'color':'red'})
lyr.renderer().setSymbol(sym)
lyr.triggerRepaint()

line_style and color are case-sensitive. Don't use Line_style and Color.

enter image description here

3
  • Thank you for tour answer, but when i run tour code it gives me this errer: AttributeError: 'QgsRuleBasedRenderer' object has no attribute 'setSymbol' And every time i try to change it ,like instead of setsymbol i write only symbol . Sometimes this error ut shows up with renderer.
    – Sanaa Ben
    Commented Jan 24, 2023 at 20:21
  • You didn't mention the layer had Rule-based symbology. The answer is about "Single Symbol" Commented Jan 24, 2023 at 20:52
  • Thank you @Kadir for tour help , it's guide me getter to identify the problem with my code . I still have one question : How to change renderer type from Rule-based to single symbol? Or how to directly change the symbology of a Rule-bases
    – Sanaa Ben
    Commented Jan 28, 2023 at 11:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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