5

I'm working on a project where I have to create different maps for the same area. I have lots of maps and each of them is related to a different theme, that I've grouped into different groups of layers as you can see in the image. Each group of layers has a name that will be used as the map title. Because I have a lot of them, each time that a select a different group I want to change the map title automatically to have the same name as the layer group that is selected.

How I can do that?

enter image description here

5

For QGIS 2.18.x

In your composer label, go to Item Properties > Insert an expression.... Click the Function Editor tab and either create a new file or use an existing one (these are scripts which can execute some code). Use something like the following code:

from qgis.core import *
from qgis.gui import *

@qgsfunction(args='auto', group='Custom')
def func(feature, parent):
    root = QgsProject.instance().layerTreeRoot()
    layer = qgis.utils.iface.activeLayer()
    lyr = root.findLayer(QgsMapLayerRegistry.instance().mapLayersByName(layer.name())[0].id())
    group = lyr.parent().name()
    if group != '':
        return lyr.parent().name()
    else:
        return 'Group not found'

Then click Load. Switch to the Expression tab and type the name of the function, which in this example is:

func()

Hopefully now, when you click on a layer, the name of the group should show up.

Screenshot


Note: you may need to click on the composer window for the name to be refreshed.



For QGIS 3.x

Similar to the above method but in the code, replace:

QgsMapLayerRegistry

with:

QgsProject

So the code should read:

from qgis.core import *
from qgis.gui import *

@qgsfunction(args='auto', group='Custom')
def func(feature, parent):
    root = QgsProject.instance().layerTreeRoot()
    layer = qgis.utils.iface.activeLayer()
    lyr = root.findLayer(QgsProject.instance().mapLayersByName(layer.name())[0].id())
    group = lyr.parent().name()
    if group != '':
        return lyr.parent().name()
    else:
        return 'Group not found'
  • HI Joseph, thanks. I did what you indicated, but doesn't work. I did the function and after that I put the expression, but this mensage appears: "the expression is invalid". I refresh the qgis composer and I select and unselect the layer group, but nothing happens, in the title just appears the expression text "[%func()%]" – mauricio santos May 30 '18 at 12:25
  • @mauriciosantos - Did you click Load? The expression text [%func()%] is correct but it should display the group names. Which QGIS version and platform are you using? – Joseph May 30 '18 at 12:37
  • Yes, i click the Load. I am using Qgis 3.0. – mauricio santos May 30 '18 at 13:02
  • @mauriciosantos - Then that may be the problem :). The solution posted works for QGIS 2.18, The name "Print Composer" changed to "Print Layout" in QGIS 3 hence why I assumed you were using the former. I'll edit the post if I can get it to work :) – Joseph May 30 '18 at 13:10
  • @mauriciosantos - Fortunately it was a simple fix :). Replace QgsMapLayerRegistry with QgsProject and all should be well. – Joseph May 30 '18 at 13:23

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.