[Qgis-developer] PyQgis crashes when using QgsComposition for printing

Marcelo Reyes reyesmarcelo at gmail.com
Wed May 19 03:06:41 EDT 2010


Hello,

I'm developping a PyQgis Stand Alone App using Windows XP and Qgis
Enceladus. I was trying to add a function that prints the map to a pdf file.
I found this code:



# set up composition
mapRenderer = iface.mapCanvas().mapRenderer()
c = QgsComposition(mapRenderer)
c.setPlotStyle(QgsComposition.Print)

dpi = c.printResolution()
dpmm = dpi / 25.4
width = int(dpmm * c.paperWidth())
height = int(dpmm * c.paperHeight())

# add a map to the composition
x, y = 0, 0
w, h = c.paperWidth(), c.paperHeight()
composerMap = QgsComposerMap(c, x,y,w,h)
c.addItem(composerMap)

# create output image and initialize it
image = QImage(QSize(width, height), QImage.Format_ARGB32)
image.setDotsPerMeterX(dpmm * 1000)
image.setDotsPerMeterY(dpmm * 1000)
image.fill(0)

# render the composition
imagePainter = QPainter(image)
sourceArea = QRectF(0, 0, c.paperWidth(), c.paperHeight())
targetArea = QRectF(0, 0, width, height)
c.render(imagePainter, targetArea, sourceArea)
imagePainter.end()

image.save("out.png", "png")
It doesn't quite work, the file out.png gets created but the application
crashes a few seconds later. I'm using the code as it is with one
modification, instead of :

mapRenderer = iface.mapCanvas().mapRenderer()
I use:

mapRenderer =  self.canvas.mapRenderer()

The line that causes the crash is: c.addItem(composerMap)
I tried to look for addItem in the QgsComposition functions list (in Qgis
API) but could not find it.

As always I'll be grateful for any advice.

Thanks,

Marcelo Reyes
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.osgeo.org/pipermail/qgis-developer/attachments/20100519/ff07865c/attachment.html


More information about the Qgis-developer mailing list