I have created a line shapefile layer (memory layer) created in Python Console of QGIS. Now I want to define the thickness of the line features that are being created. I am aware of the method by which the line styles/symbology can be imported from the existing '.qml' file.

self.setLayerSymbology(vlayer, "Parent_PermanentMarks.qml") 

Is there any way by which symbology (line thickness) of the lines can be defined 'on the go' via python console?

  • 5
    For shure it is. Did you read the developer cookbook before asking this question? docs.qgis.org/testing/en/docs/pyqgis_developer_cookbook/… Oct 18, 2016 at 11:52
  • @JochenSchwarze ya thank you. Got that.. "symbol.setWidth(0.46)"
    – Sjs
    Oct 19, 2016 at 6:43
  • 3
    Welcome to gis.stackexchange! Please note that a good question on this site is expected to show some degree of research on your part, i.e. what you have tried and - if applicable - code so far. For more info, you can check our faq.
    – underdark
    Oct 27, 2016 at 8:51
  • 1
    I'm voting to close this question as off-topic because it shows zero research effort. Oct 27, 2016 at 14:44

2 Answers 2


The below script worked for me.

from qgis.utils import iface
from qgis.core import *
from qgis.utils import *
from qgis.gui import *

vlayer = QgsVectorLayer("C:\SampleData\world_borders_lines.shp", "world_borders", "ogr")
props = { 'width' : '2', 'color' : '0,0,255' }
sl = QgsSymbolLayerV2Registry.instance().symbolLayerMetadata("SimpleLine").createSymbolLayer(props)
s = QgsLineSymbolV2([sl])
vlayer.setRendererV2( QgsSingleSymbolRendererV2( s ) )
  • 1
    That worked. Thank you. Only change I had to make is to set the path to shapefile like "D:\\abs\\path\\Lines.shp"
    – Sjs
    Oct 20, 2016 at 6:45

Pen width (only line width, not other parameters such as line color) can be set using


Full code (Set Line width for the line shapefile layer being loaded):

vlayer = QgsVectorLayer("D:\\abs\\path\\Lines.shp", "LineLayer", "ogr")        
symbols = vlayer.rendererV2().symbols()
symbol = symbols[0]

NB: To change line color along with pen width, check out the other answer to this question.

  • Please edit your answer to explain why the code you posted answers your question.
    – Fezter
    Oct 20, 2016 at 7:05

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.