2

I'm trying to set up style properties of a vector layer using python. I was able to modify the marker properties but I cannot modify those of Simple Marker, like width or color. Here is my code :

  layer=iface.activeLayer()

  # Marker

  single_symbol_renderer=layer.renderer()
  symbol=single_symbol_renderer.symbol()
  symbol.setColor(QColor.fromRgb(255,255,1))
  symbol.setSize(4)

  # Simple Marker

  marker_symb = QgsSimpleMarkerSymbolLayer()
  marker_symb.setStrokeColor(QColor(255,255,1))
  marker_symb.setStrokeWidth(0.9)
  layer.subSymbol(marker_symb)
  layer.triggerRepaint()

Please can you help me ?

1

You can use a similar method for the marker but by adding symbol.symbolLayer(0) to identify the initial simple marker layer:

layer = iface.activeLayer()
single_symbol_renderer = layer.renderer()
symbol = single_symbol_renderer.symbol()
symbol.symbolLayer(0).setStrokeColor(QColor(255,255,1))
symbol.symbolLayer(0).setStrokeWidth(0.9)

layer.triggerRepaint()
iface.layerTreeView().refreshLayerSymbology(layer.id())
3
  • Thanks you so much ! Was tkinking about a solution for several days ! – Valentin L. Jun 6 '19 at 13:36
  • I have one more question if you don't mind. It's about placement of labels. I would like to place them with "Offset from point" and define OffsetX and Offset Y. I tkink you have to use QgsPalLayerSettings but, in spite of my researches, i don't have find the solution to do it. – Valentin L. Jun 6 '19 at 13:48
  • @ValentinL. - Most welcome! Glad it helped :). I suggest you ask this as a new question. This would make it easier for people with similar problems to find a possible solution. – Joseph Jun 7 '19 at 10:16

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.