Saturday, 14 April 2018

labeling - How to turn on/off all labels of all layers in QGIS


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?



Answer



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


No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...