I have a script that was working, where I was able to grab a composer label by its ID and then update its text by doing the following:
labelTitle = myComposition.getComposerItemById('pageTitle')
labelTitle.setText('New Title')
But after updating to the latest version of QGIS, it looks like labelTitle is coming back as a QgsComposerItem type instead of QgsComposerLabel, so it is throwing the error 'QgsComposerItem' object has no attribute 'setText'
Is there a way to cast it as a label, or am I missing something? Or is there an entirely better way to accomplish this?
Answer
Another workaround suggested by Nyall Dawson (cast) :
from qgis.core import QgsProject, QgsLayoutItemPicture
import sip
myLayout = QgsProject.instance().layoutManager().layoutByName('test')
myLayoutPicture = sip.cast(myLayout.itemById("picture"), QgsLayoutItemPicture)
myLayoutPicture.setPicturePath("path to an image")
No comments:
Post a Comment