0

I'm trying to migrate from QGIS API 2.18 to 3.2 in Ubuntu18. Several changes are made, including the update from PyQT4 to PyQT5. This is the very beginning of my code:

from qgis.core import QgsApplication, QgsProject
from qgis.gui import QgsMapCanvas,  QgsLayerTreeMapCanvasBridge
from PyQt5.QtCore import QFileInfo
from PyQt5.QtXml import QDomDocument

reportDir = ''
project_path = reportDir+'/miQgsVegProject2.qgs'
template_path = reportDir+'/miQptVegProject2.qpt'

QgsApplication.setPrefixPath("/usr/share/qgis", True)
app = QgsApplication([], False)
app.initQgis()

canvas = QgsMapCanvas()
project = QgsProject.instance()

project.readProject(QFileInfo(project_path))
layers=QgsProject.instance().mapLayers()
bridge = QgsLayerTreeMapCanvasBridge(project.layerTreeRoot(), canvas)
bridge.setCanvasLayers()
template_file = file(template_path)
template_content = template_file.read()
template_file.close()
document = QDomDocument()
document.setContent(template_content)
composition = QgsComposition(canvas.mapSettings())
composition = QgsComposition(canvas.mapRenderer())
composition.setPlotStyle(QgsComposition.Print)

composition.loadFromTemplate(document, {})
template_file = file(template_path)
template_content = template_file.read()
template_file.close()
document = QDomDocument()
document.setContent(template_content)
composition = QgsComposition(canvas.mapSettings())
composition = QgsComposition(canvas.mapRenderer())
composition.setPlotStyle(QgsComposition.Print)

composition.loadFromTemplate(document, {})

map_item = composition.getComposerItemById('mapa')
map_item.setMapCanvas(canvas)
map_item.zoomToExtent(canvas.extent())
#map_item.zoomToExtent(miExtent)


composition.refreshItems()
composition.setPlotStyle(QgsComposition.Preview)
composition.setPrintAsRaster(True)
composition.exportAsPDF(pdfOutput)
QgsProject.instance().clear()


app.exitQgis()

This tiny code results in:

project.readProject(QFileInfo(project_path))
TypeError: native Qt signal is not callable

At PyQT4 this works but I can't find a clue to migrate correctly with PyQT5.

0

You're trying to call a signal, which isn't possible. The method you're looking for is:

if not project.read(project_path):
    print('Uhuh - something went wrong!')
  • Ok. It seems I miss why it works in PyQT4 and not in PyQT5. Do you have any idea to load the project, or how to correct the code? – Alfredo Aug 7 '18 at 2:28
  • The code in the answer I posted is what you should use. – ndawson Aug 7 '18 at 4:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.