I am creating a QgsVectorLayer in a plugin and loading it into the map. I would like to set the labelling parameters while I'm doing that. I've followed several examples found here and here, but my labels aren't being displayed. When I go to the labelling tool none of the settings are as I've specified.

def finalize(self):

def setDisplayProperties(self):
    palyr = QgsPalLayerSettings()
    palyr.enabled = True       #this works
    palyr.fieldName = 'label'  #this works
    palyr.placement = QgsPalLayerSettings.OverPoint  #??
    palyr.placementFlags = palyr.placementFlags or QgsPalLayerSettings(BelowLine)    # not working
    palyr.fontSizeInMapUnits = False
    palyr.textFont.setPointSize(4.6)  #results in 4 - seems to be integer only
    palyr.textColor = QColor(68,92,249) #this works

I've tried calling setDisplayProperties at every slot in the finalize procedure with the same result every time. I've also tried with and without various settings.

  • Do you receive any errors from the Python Console when you run the above code? Also, you're labelling layer but you're adding vl to the canvas. Are they meant to be different? – Joseph Jul 5 '16 at 12:02
  • 1
    @Joseph good pick, but changing it to vl only had partial improvements marked above. There are no Python errors – marcp Jul 5 '16 at 16:46
  • For future readers, Joseph and @artwork21 found typos in my code. I've incorporated their changes above, which is now a working example. – marcp Jul 6 '16 at 21:23

The "c" in textcolor needs to be upper case like:

palyr.textColor = QColor(68,92,249)

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.