5

I'm new to using Python to automate stuff in QGIS. I've used the following code to create a new map composer in QGIS:

# create a new composer 
c1test = iface.createNewComposer("mon premier composeur")
# the command above is successful : a new composer window appears

# the variable composeur1 will contain the composer itself
composeur1 = c1test.composition()

# x1, y1, x2, y2 are the dimensions of the composer.
x1,y1 = 0,0
x2,y2 = composeur1.paperWidth(),composeur1.paperHeight()

# I create a map item that will span across the whole area of the composer, 
# and I add it to the composer 
carte_composeur =QgsComposerMap(composeur1,x1,y1,x2,y2)
composeur1.addItem(carte_composeur)

# refresh
composeur1.refreshItems

As you can see on the screenshot below, the map item is present in the composer, but it's blank. There is only a message "The map will be printed here" (in French in my case). Moreover, the panel "Item Properties" on the right-hand side is empty.

So my question is: how can I force my map item to be binded to the main QGIS canvas, as it is usually the case with map composers?

PS: I am aware of this similar thread but I didn't understand the answer and wasn't able to apply it in my case.

The map item is present, but blank

Kadir Şahbaz
  • 76,800
  • 56
  • 247
  • 389
R. Bourgeon
  • 183
  • 5

1 Answers1

6

Change composeur1.addItem(carte_composeur) into composeur1.addComposerMap(carte_composeur).

I expected addItem to add map item passed, but it didn't. I suggest you to review addComposer***() functions in QGIS API Documentation.

Kadir Şahbaz
  • 76,800
  • 56
  • 247
  • 389