Accessing composer item properties via custom expressions in QGIS

So here is a neat trick. Lets say you wanted to access the scale of a composer map to make it part of a label. The scale bar can already be set to numeric to show the number value but what if it needs to be part of an existing label with other text. Not to fear, expression functions are here.

  • Create a new composer. Add the map frame and a label.
  • Set the item ID of the map frame to something you can remember, lets just use themap
  • Select the label and add some text
  • Click Insert Expression

Now for the cool part

  • Select Function Editor
  • Click New File. Give the file a new name and hit save. I called it composer functions.

In the code editor paste this code:

from qgis.utils import iface
from qgis.core import *
from qgis.gui import *

@qgsfunction(args="auto", group='Composer')
def composeritemattr(composername, mapname, attrname, feature, parent):
    composers = iface.activeComposers()
    # Find the composer with the given name
    comp = [composer.composition() for composer in composers 
                if composer.composerWindow().windowTitle() == composername][0]
    # Find the item
    item = comp.getComposerItemById(mapname)
    # Get the attr by name and call 
    return getattr(item, attrname)()
  • Click Run Script


Now in your label use this text:

Scale: [% composeritemattr('Composer 1', 'themap', 'scale')%]

Update the Composer 1 to match your composer name, and the themap to match your item ID.

and like magic here is the scale from the map item in a label:

2015-05-21 22_00_09-Composer 1

Check the expression error section if the label doesn’t render


9 thoughts on “Accessing composer item properties via custom expressions in QGIS

  1. I just get the string Scale: [% composermapattr(‘Composer 1’, ‘themap’, ‘scale’)%] in the text box

      1. I hadn’t but have now and still only the string – do I have to change any of the parameters in the function to match Item ID or anything?
        Just been through it again afresh but still nothing!

    1. Cheers Nat – I found the problem: in my label string the composeritemattr had underscores so it looked like this: composer_item_mattr – don’t ask me why but there you are!
      The other question is around colours for attribute grids in a layout – you can change the header font and colour, is there a way of changing the background colour of the header different to the body?
      It is a frustration of mine – speaking at the UK user group tomorrow on this, that the styling for tables seems to be lacking so you can’t really get it looking good – presentation is a big part of what we do and I’m competing against desktop publishers!

      Have some ideas so if you’re interested we can have a discussion on the subject.

    2. One more thing! Can you return the scale to a whole number as scales arn’t expressed with decimals?

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s