1

I have the following script which has worked on Ubuntu 16.04 and Windows but not on Debian.

from PyQt4.QtCore import *
from PyQt4.QtGui import *
from PyQt4.QtSql import *
from PyQt4.QtXml import QDomDocument
from qgis.core import *
from qgis.gui import *

qpt_file = "a4_vertical.qpt"
output_html = "<html><body>test</body></html>"
if QGis.QGIS_VERSION_INT > 20400:
    renderer = iface.mapCanvas().mapSettings()
else:
    renderer = iface.mapCanvas().mapRenderer()

c = QgsComposition(renderer)

qpt_file_dom = QDomDocument()
qpt_file_dom.setContent(QFile(qpt_file), False)
c.loadFromTemplate(qpt_file_dom, None)
c.refreshItems()

composerMap = c.getComposerMapById(0)

composerLabel = c.getComposerItemById('HEADER')
composerLabel.setText(output_html)

When running the code on Debian I get the following error:

Traceback (most recent call last):
  File "<input>", line 1, in <module>
  File "/tmp/tmpB8icUm.py", line 26, in <module>
    composerLabel.setText(output_html)
AttributeError: 'QgsComposerItem' object has no attribute 'setText'

I tried to solve this based on this issue which is 6 years old: https://issues.qgis.org/issues/3236

It works when I add composerLabel.__class__ = QgsComposerLabel.

But when I'm trying this with QgsComposerFrame as follows:

table_frame = comp.getComposerItemById('HTML_FRAME_1')
if not isinstance(table_frame, QgsComposerFrame):
    table_frame.__class__ = QgsComposerFrame
table_frame = table_frame.multiFrame()

I get the AttributeError:

AttributeError: 'QgsComposerFrame' object has no attribute 'multiFrame'

I'm running Qgis 2.14.18-Essen on Debian Stretch.

QGIS version information:

QGIS version: 2.14.18-Essen
QGIS code branch: Release 2.14
Compiled against Qt: 4.8.7
Running against Qt: 4.8.7
Compiled against GDAL/OGR: 2.1.2
Running against GDAL/OGR: 2.1.2
Compiled against GEOS: 3.5.1-CAPI-1.9.1
Running against GEOS: 3.5.1-CAPI-1.9.1 r4246
PostgreSQL Client Version: 9.6.3
SpatiaLite Version: 4.3.0a
QWT Version: 6.1.2
PROJ.4 Version: 493
QScintilla2 Version: 2.9.3

I tried run this code under Ubuntu 16.04 and Windows 7 and there it runs without problem.

Your Answer

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

Browse other questions tagged or ask your own question.