5

I would like to turn on / turn off all labels of any layer by one klick. Like you can do in ArcGIS according to that post:

Is there a way to turn off all labels in ArcMap without clicking on every layer?

8

Not sure if there is an existing option to do this but using a bit of python, we can always create such a function ourselves!

So we could:

  1. Create a temporary icon on the toolbar which can toggle.
  2. Create a function which enables/disables labeling for each layer depending on the toggle state of the icon.
  3. Connect the icon to the function.

The code could look something like this which you can paste into the Python Console:

from PyQt4.QtCore import QObject, SIGNAL
from PyQt4.QtGui import QAction, QIcon

action = QAction(QIcon(""), "Turn labels" + "\n" + "ON/OFF", iface.mainWindow())
action.setCheckable(True)
iface.addToolBarIcon(action)

def label_control():
    for layer in QgsMapLayerRegistry.instance().mapLayers().values():       
        if action.isChecked() == True:      
            layer.setCustomProperty("labeling/enabled", True)
        else:
            layer.setCustomProperty("labeling/enabled", False)
        layer.triggerRepaint()

QObject.connect(action, SIGNAL("triggered()"), label_control)
# Uncomment line below if you want to remove the icon yourself,
# otherwise it will be removed automatically when you restart QGIS
#iface.removeToolBarIcon(action)

Example:

Couple of layers which have labelling enabled (toggled):

Enabling labels

Same layers but with the labels disabled (untoggled):

Disabling labels

  • 1
    great solution! – matteo Jan 30 '17 at 15:09
  • Fine, that's it.... but I have no clue, how to do so... BTW: interesting point layer :-) – MAP Jan 30 '17 at 15:10
  • @matteo - Thanks, used something similar in a plugin as I found it useful for my particular use-case :) – Joseph Jan 30 '17 at 15:12
  • @MAP - It does not work for you? Also yes, ignore the point layer :) – Joseph Jan 30 '17 at 15:14
  • 1
    @MAP - I've asked a new question concerning rule-based labels as I couldn't find much information as to how we can control them using pyqgis. – Joseph Jan 31 '17 at 11:36

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.