1

A pyqgis stand-alone app labels layer features with QgsPalLayerSettings, in the same fashion as in Labelling line layer using QgsPalLayerSettings() QGIS failed, it works for me too.

How do I retrieve that pal layer object after it is "tied in" with a layer for further manipulation (enable/disable etc) or do I just define another pal layer object and writeTo that same layer again to overwrite it?

there is a fromLayer(layer) static function of the QgsPalLayerSettings class that seems to be able to get back a PalLayer object, but it looks like it is a cloned copy, I would like to just get the original pal layer object.

1

Don't think you need to define another pal object. Just use the readFromLayer() method once and then you can retrieve your labelling properties or update them.


For example, if you want to see if the labelling properties are currently enabled/disabled or see what field is chosen, you could use:

palyr = QgsPalLayerSettings()
layer = iface.activeLayer()
palyr.readFromLayer(layer)

palyr.enabled
palyr.fieldName

Which for me printed:

>>>False
>>>u''

If you want to overwrite the same settings, you could use:

palyr = QgsPalLayerSettings()
layer = iface.activeLayer()
palyr.readFromLayer(layer)

def update():
    palyr.writeToLayer(layer)
    layer.triggerRepaint()

palyr.enabled = True
palyr.fieldName = 'City'
update()

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.