Bug report #17180

Atlas crash using python

Added by Loïc BARTOLETTI almost 3 years ago. Updated over 1 year ago.

Status:Closed
Priority:High
Assignee:-
Category:Python bindings / sipify
Affected QGIS version:2.18.13 Regression?:No
Operating System: Easy fix?:No
Pull Request or Patch supplied:No Resolution:
Crashes QGIS or corrupts data:Yes Copied to github as #:25079

Description

Hi,

I asked a question on https://gis.stackexchange.com/questions/256143/duplicate-composition-with-python-qgis about a need to duplicate a composer and deal with it in python.

It's maybe a bad code from me, but since it crashes QGis (tested on Windows and FreeBSD), I open a ticket here to let you know.

Thanks.


Related issues

Related to QGIS Application - Bug report #17181: Crash using QgsComposerAttributeTableV2 after setting col... Closed 2017-09-21

History

#1 Updated by Giovanni Manghi almost 3 years ago

  • Status changed from Open to Feedback

This ticket (#17180) or this #17181 ?

#3 Updated by Giovanni Manghi almost 3 years ago

  • Status changed from Feedback to Open

Please add a description.

#4 Updated by Loïc BARTOLETTI almost 3 years ago

For this issue, when you use an invalid output dir, you don't have a crash but it's return my print debug "exportAsPdf", so I suspect a problem into this method (can't see or produce logs now).

#5 Updated by Loïc BARTOLETTI almost 3 years ago

Since it's not available in Qgis 2, I'm trying to duplicate a composition with pyqgis translating this code from Qgis 3.
Duplication is fine, but every time I want to export an atlas of this new composition, qgis crash. Do you have any idea?

Here is a reproductible example of my code (using with python console and a small project):

    from qgis.PyQt.QtXml import *
    from qgis.PyQt.QtGui import *
    from qgis.PyQt.QtWidgets import *
    from qgis.PyQt.QtCore import *

    import os

    def saveAsTemplate(composition):
        doc = QDomDocument()
        composerElem = doc.createElement(  "Composer"  )
        doc.appendChild( composerElem )
        composition.writeXML( composerElem, doc )
        composition.atlasComposition().writeXML( composerElem, doc )
        return doc

    def duplicateComposition(composition):
        currentDoc = saveAsTemplate(composition)
        compositionElem = currentDoc.documentElement().firstChildElement( "Composition" )
        if compositionElem.isNull():
            print( "selected composer could not be stored as temporary template" )
            return None

        newComposition = QgsComposition(composition.mapSettings())

        if not newComposition.loadFromTemplate(currentDoc):
            print( " Cannot load template ")
            del newComposition
            return None

        return newComposition

    myComposition = iface.activeComposers()[0].composition()
    newComposition = duplicateComposition(myComposition)

    atlas = newComposition.atlasComposition()
    atlasLayer = iface.activeLayer()
    atlas.setCoverageLayer(atlasLayer)
    atlas.setPageNameExpression(atlasLayer.name())
    atlas.setEnabled( True )
    ret = newComposition.setAtlasMode( QgsComposition.ExportAtlas )
    if ret is False:
        print "Error" 

    outputDir = "C:/temp/export_atlas" 
    atlas.beginRender()
    print("atlas")
    num = atlas.numFeatures()
    for i in range(0, num):
        ret = atlas.prepareForFeature( i )
        if ret is False:
            print "prepareForFeatureror" 
        printer = QPrinter()
        printer.setOutputFormat(QPrinter.PdfFormat)
        ret = newComposition.exportAsPDF(os.path.join(outputDir, str(i)+".pdf"))
        if ret is False:
            print "exportAsPDF" 

    atlas.endRender()

Original message

#6 Updated by Jürgen Fischer almost 3 years ago

  • Related to Bug report #17181: Crash using QgsComposerAttributeTableV2 after setting columns added

#7 Updated by Denis Rouzaud almost 3 years ago

  • Assignee deleted (Denis Rouzaud)

#8 Updated by Giovanni Manghi over 2 years ago

  • Priority changed from Normal to High

#9 Updated by Jürgen Fischer over 1 year ago

  • Status changed from Open to Feedback

Please test with QGIS 3.4 - QGIS 2.18 reached it's end of life.

#10 Updated by Loïc BARTOLETTI over 1 year ago

  • Status changed from Feedback to Closed

Also available in: Atom PDF