5

I have been trying to figure out how to set the transparency of my labels and label buffers in PyQGIS for an application I am building. I am using the "setCustomProperty" class from the QGIS API. I can't find anything in the API documentation that allows for setting transparency. This is possible in the QGIS GUI via a slider, but seemingly impossible in PyQGIS. Here is a snippet of my labeling code:

    self.mylayer.setCustomProperty("labeling", "pal")
    self.mylayer.setCustomProperty("labeling/isExpression", True)
    self.mylayer.setCustomProperty("labeling/enabled", True)
    self.mylayer.setCustomProperty("labeling/bufferDraw", True)
    self.mylayer.setCustomProperty("labeling/bufferSize", "1")
    self.mylayer.setCustomProperty("labeling/bufferColor", "white")
    self.mylayer.setCustomProperty("labeling/fontFamily", "Arial")

Shouldn't there be a custom property setting for transparency? Like a bufferAlpha or a textAlpha setting or something?

3

textTransp has worked for me. You need to give it a value (string, integer, or float) between 0 and 100 (like a percentage):

layer = iface.activeLayer()
layer.setCustomProperty("labeling", "pal")
layer.setCustomProperty("labeling/enabled", "true")
layer.setCustomProperty("labeling/fontFamily", "Arial")
layer.setCustomProperty("labeling/fontSize", "10")
layer.setCustomProperty("labeling/fieldName", "NAME")
layer.setCustomProperty("labeling/textTransp", "70")
iface.mapCanvas().refresh()

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.