[GIS] Adding label to QGIS map composer through Python script

print-composerpyqgisqgis

I need help in adding a label to QGIS map composer through python script. I could add a label but it is incomplete. The label is like this: 27-02-15 11-25. It only shows the first part: 27-02-15. I don't know why.

Also when I create the composer map, the other elements were put outside of the composer map rectangle. It is possible to overlap elements in map composer?

EDIT
I see that the problem is in the width and height of label box. I can't find a way of change this parameters. The adjustSizeToText should do it, but somehow is not doing that.

Best Answer

Ok. My bad. The error was mine.

The adjustSizeToText must be in the final of the definition of characteristics of the text. Sorry for that. If anyone has this problem, please put the adjustSizeTotext in the end, like this:

title = QgsComposerLabel(myComposition)
title.setText(str(text))
title.setFont(QFont("Cambria",40, QFont.Bold))
title.setItemPosition(185.8,5.2)
title.adjustSizeToText()
myComposition.addItem(title)  
Related Question