3

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")
  • Have you tried using 75 instead of "75" and True instead of "True"? Also, I believe "labeling/weight" isn't supported (only "labeling/fontWeight"). – ArMoraer Apr 29 '16 at 8:11
  • Thanks. I think removing the quotes is a good idea. Unfortunately, this doesn't solve the problem. – Steven Apr 29 '16 at 8:22
  • 1
    Great! This works. – Steven May 5 '16 at 18:52
  • Just posted my comment as an answer. Great that you've solved the problem. – Germán Carrillo May 6 '16 at 13:21
1

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!

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.