6

I'm only new to QGIS and Python so this may exists already, and hopefully be easy to do.

What I'm hoping to do sometime in the future is build a map book generator using QGIS as the base. In order to do this I need to be able to select all the labels in the composer and replace some tags, so something like $NAME$ would replace with a variable the user has set at runtime.

I have been having a look at the QGIS APIs [and source] but just can't seem to get it to do what I need. The code I have so far:

composerList = self.iface.activeComposers()
if(len(composerList) < 1):
      return

composerView = composerList[0]
composition = composerView.composition()
if(composition is None):
      return

for item in composition.selectedComposerItems():
//Need to check for QgsComposerLabel type and update the text.

I'm stuck when it comes to checking the type of the current item in Python, selectedComposerItems() just returns each item as a QgsComposerItem and calling displayText() fails because it doesn't exists on the type QgsComposerItem.

I have tried the following code but it crashes QGIS when it hits a legend or scale bar:

for item in composition.selectedComposerItems():
    try:
        item.__class__ = qgis.core.QgsComposerLabel
        if item.displayText() == "$NAME$":
            print "I found one"
            item.setText("Hello World")
    except:
        print "Fail"

The idea was to just try setting __class__ on every item as qgis.core.QgsComposerLabel and if it throws an exception then I know it's not a label, but at the moment if it hits a legend or scale bar it crashes QGIS which is not good.

  • Is your problem solved now? – underdark Dec 21 '10 at 8:56
  • I think we should preserve this question for historical interest. I had forgotten there was a time when you could truthfully say "I'm only new to QGIS and Python". – PolyGeo Sep 9 '17 at 8:38
4
type(item).__name__

should return 'QgsComposerLabel', but only returns 'QgsComposerItem'.

There's now a ticket in QGIS Trac.

  • yeah I did give that a go. I was going to open a ticket a while ago but had no idea if it was a bug or just me not doing it right. – Nathan W Nov 19 '10 at 11:10
  • I think it's a bug. Let's see what we get as response to the bug report. – underdark Nov 19 '10 at 11:22
  • The bug has been fixed now: trac.osgeo.org/qgis/ticket/3236#comment:4 – underdark Nov 25 '10 at 22:03
0

Thanks it was helpful but item.displayText() irritated me as it's never a good idea to get object by display text rather get it by id this way you don't even need to check for class of object. just define id on print composer then dynamically change it as required using code below.

        try:
            #print item.id
            if item.id()=="build_id":
            print "found id"
            item.setText(current_gid)
        except:
            print "Fail"
-1

This is one option

    item2= composition.getComposerItemById('current_year_is')
    item2.setText(current_year)

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.