1

I'm trying to turn off QGIS3 (3.2) labels via pyqgis per How to turn on/off all labels of all layers in QGIS . I adapted the code in QGIS 2 answers linked above, as follows:

#...
def toggleAllLabels(self):
    for layer in QgsProject.instance().mapLayers().values():       
        if self.toggleLabelsAction.isChecked() == True:      
            layer.setCustomProperty("labeling/enabled", True)
        else:
            layer.setCustomProperty("labeling/enabled", False)
        layer.triggerRepaint()

But this doesn't work to turn off the labels. The layers refreshes, but the labels remain.

Is layer.setCustomProperty("labeling/enabled", False) still the right way to turn off layer labels in QGIS 3? (Or, what's the current way?)

1

There's a setLabelsEnabled method on layers. Here's me disabling labels on the first layer on my map canvas from the python console:

iface.mapCanvas().layers()[0].setLabelsEnabled(False)
iface.mapCanvas().layers()[0].triggerRepaint()

cribbed from https://www.queryxchange.com/q/23_289302/pyqgis-3-how-to-use-data-defined-property-for-labeling/

More authoritative source here: https://github.com/qgis/QGIS/blob/master/doc/api_break.dox#L364

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.