3

I'm trying to modify the "Decimal places" (defaut : 3) to "1" from the formatting layer properties.

enter image description here

Here is my code :

background_color = QgsTextBackgroundSettings()
background_color.setEnabled(True)

prop = QgsProperty()
prop.setField("Couleur")
pc = QgsPropertyCollection('ShapeFillColor')
pc.setProperty(58, prop)
layer_settings = QgsPalLayerSettings()
layer_settings.setDataDefinedProperties(pc)

text_format = QgsTextFormat()
text_format.setFont(QFont("MS Shell Dlg 2"))
text_format.setSize(12.5)
text_format.setColor(QColor("black"))
text_format.setFont(QFont("MS Shell Dlg 2",11,QFont.Bold))
text_format.setBackground(background_color)

layer_settings.setFormat(text_format)
layer_settings.fieldName = "NO2"
layer_settings.enabled = True

I think I have to use a function of the class QgsTextFormat but I can’t find which one in the documentary.

1 Answer 1

1

Logically, it would be this code, but the checkbox Formatted Numbers isn't checked on my QGIS 3.30 (Windows) :

lyr_no2_concentration = iface.activeLayer()
lyr_settings = QgsPalLayerSettings()
lyr_settings.enabled = True
lyr_settings.fieldName = "NO2"
# code to activate the decimals
lyr_settings.formatNumbers = True  # checkbox
lyr_settings.decimals = 2  # decimals number
# -----------------------------
lyr_no2_concentration.setLabeling(
    QgsVectorLayerSimpleLabeling(lyr_settings)
)
lyr_no2_concentration.triggerRepaint()

See the QgsPalLayerSettings documentation for more details.

Your Answer

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

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