<div>Hello,</div>
<div> </div>
<div>I&#39;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:</div>
<div> </div>
<div><br><br># set up composition <br>mapRenderer = iface.mapCanvas().mapRenderer() <br>c = QgsComposition(mapRenderer) <br>c.setPlotStyle(QgsComposition.Print) <br><br>dpi = c.printResolution() <br>dpmm = dpi / 25.4 <br>
width = int(dpmm * c.paperWidth()) <br>height = int(dpmm * c.paperHeight()) <br><br># add a map to the composition <br>x, y = 0, 0 <br>w, h = c.paperWidth(), c.paperHeight() <br>composerMap = QgsComposerMap(c, x,y,w,h) <br>
c.addItem(composerMap) <br><br># create output image and initialize it <br>image = QImage(QSize(width, height), QImage.Format_ARGB32) <br>image.setDotsPerMeterX(dpmm * 1000) <br>image.setDotsPerMeterY(dpmm * 1000) <br>image.fill(0) <br>
<br># render the composition <br>imagePainter = QPainter(image) <br>sourceArea = QRectF(0, 0, c.paperWidth(), c.paperHeight()) <br>targetArea = QRectF(0, 0, width, height) <br>c.render(imagePainter, targetArea, sourceArea) <br>
imagePainter.end() <br><br>image.save(&quot;out.png&quot;, &quot;png&quot;) <br></div>
<div>It doesn&#39;t quite work, the file out.png gets created but the application crashes a few seconds later. I&#39;m using the code as it is with one modification, instead of :</div>
<div> </div>
<div>mapRenderer = iface.mapCanvas().mapRenderer() <br></div>
<div>I use: </div>
<div> </div>
<div>mapRenderer =  self.canvas.mapRenderer()</div>
<div> </div>
<div>The line that causes the crash is: c.addItem(composerMap) <br></div>
<div>I tried to look for addItem in the QgsComposition functions list (in Qgis API) but could not find it.</div>
<div> </div>
<div>As always I&#39;ll be grateful for any advice.</div>
<div> </div>
<div>Thanks,</div>
<div> </div>
<div>Marcelo Reyes<br clear="all"><br> <br></div>