2

I'm trying to get a "P" font marker in the middle of my polygon layer. So far, I've managed to make a simple centroid marker (the basic one) appear on my polygons.

The code doesn't bug, but it doesn't change the simple marker to a font marker.

Here's the code, does anyone know what I am missing?

colorpark = QtGui.QColor(88, 88, 88, 255)
symbo_park = QgsSimpleFillSymbolLayer(color=colorpark, strokeColor=colorpark, strokeWidth=0)
parking_l93.renderer().symbol().changeSymbolLayer(0, symbo_park)
parking_l93.triggerRepaint()

symbol_parkcent = QgsCentroidFillSymbolLayer.create()
parking_l93.renderer().symbol().appendSymbolLayer(symbol_parkcent)
colorP = QtGui.QColor(255, 255, 255, 255) 
symbolparkP = QgsFontMarkerSymbolLayer(fontFamily='Arial Black', chr='P', pointSize=2, color=colorP)      
parking_L93.renderer().symbol().changeSymbolLayer(0, symbolparkP)                
parking_l93.triggerRepaint()
2

The QgsFontMarkerSymbolLayer must be added as a subSymbol to your QgsCentroidFillSymbolLayer.

I just needed to add 4 lines to your code to obtain something like this :

enter image description here

Here is the lines I added :

symbol_parkcent = QgsCentroidFillSymbolLayer.create()
# Make the centroid transparent
symbol_parkcent.setColor(QColor("transparent"))
#
colorP = QtGui.QColor(255, 255, 255, 255) 
symbolparkP = QgsFontMarkerSymbolLayer(fontFamily='Arial Black', chr='P', pointSize=2, color=colorP)      
# Add the Font Marker as subsymbol
marker_symbol = QgsMarkerSymbol()
marker_symbol.changeSymbolLayer(0, symbolparkP)
symbol_parkcent.setSubSymbol(marker_symbol)
#
parking_l93.renderer().symbol().appendSymbolLayer(symbol_parkcent)
parking_l93.triggerRepaint()

By using appendSymbolLayer I added your QgsFontMarkerSymbolLayer to your QgsSimpleFillSymbolLayer If you only want the QgsFontMarkerSymbolLayer you need to use parking_l93.renderer().symbol().changeSymbolLayer(0, symbol_parkcent)

1
  • Works perfectly, many thanks!
    – cam_gis
    Jul 21 at 7:55

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.