4

I'm trying to apply some labeling properties to a layer using Python. Everything went fine, except the way I tried to apply the Bold and Italic settings. Here is a part of the code:

  layer.setCustomProperty("labeling/fieldName", field.name())
  layer.setCustomProperty("labeling", "pal")
  layer.setCustomProperty("labeling/enabled", "True")
  layer.setCustomProperty("labeling/fontFamily", "Arial Narrow")

  if self.dlg.pt6.isChecked():
    layer.setCustomProperty("labeling/fontSize", "6")
  if self.dlg.pt8.isChecked():
    layer.setCustomProperty("labeling/fontSize", "8")
  if self.dlg.pt10.isChecked():
    layer.setCustomProperty("labeling/fontSize", "10")

  if self.dlg.chkbold.isChecked():
    layer.setCustomProperty("labeling/weight", "Bold")
    layer.setCustomProperty("labeling/fontWeight", "75")
    layer.setCustomProperty("labeling/Weight", "Bold")

  if self.dlg.chkitalic.isChecked():
    layer.setCustomProperty("labeling/fontItalic", "True")
Germán Carrillo
  • 36,307
  • 5
  • 123
  • 178
Steven
  • 190
  • 7

1 Answers1

2

It seems that it depends on the font you're using. Try alternating among the following options:

Bold:

layer.setCustomProperty("labeling/namedStyle", "Bold")

Italic:

layer.setCustomProperty("labeling/namedStyle", "Italic")

Bold and Italic:

layer.setCustomProperty("labeling/namedStyle", "Bold Italic")

If neither Bold or Italic are selected in your plugin, you could use:

layer.setCustomProperty("labeling/namedStyle", "Regular")

These options correspond to styles in the QGIS GUI:

enter image description here

This should do the trick!

Germán Carrillo
  • 36,307
  • 5
  • 123
  • 178