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)
            layer.setCustomProperty("labeling/enabled", False)

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?)


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


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.