5

I have a local layer which gets updated every now and then by being copied from a WFS. The local layer has some labels shown and some hidden through auxiliary table, because QGIS seems to only accept a very specific auxiliary table to show/hide labels through labels toolbar.

What I am trying to do is to create or copy the existing auxiliary table from the old local layer to the newly created local layer with python. I've been trying some methods from here. But with little success.

I know that I can also create a Boolean field in a shapefile and point there for show/hide label. But doing that makes me unable to use show/hide label button in the toolbar. So that is not ideal.

1
  • Old layer and newly created layer have the same fields' structure, right? Mar 8, 2020 at 16:53

1 Answer 1

5
+50

Change layer names in the script to your layer name, then run the script.

source_layer = QgsProject.instance().mapLayersByName("source_layer_name")[0]
target_layer = QgsProject.instance().mapLayersByName("target_layer_name")[0]

source_al = source_layer.auxiliaryLayer()
target_layer.setAuxiliaryLayer(source_al)

target_layer.setLabelsEnabled(True)
target_layer.setLabeling(source_layer.labeling())

iface.mapCanvas().refresh()

enter image description here

1
  • Thanks, this seems to do exactly what i need.
    – anjisa
    Mar 9, 2020 at 7:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.