Bug report #14826

.QML style only partly rendered into image using PyQGIS API

Added by Yury Ryabov almost 8 years ago. Updated about 5 years ago.

Affected QGIS version:2.14.2 Regression?:No
Operating System:Linux Easy fix?:No
Pull Request or Patch supplied:No Resolution:end of life
Crashes QGIS or corrupts data:No Copied to github as #:22780


I have a .QML style file for the polygon layer (see file attached) that was created in QGIS. Inside QGIS layers are rendered correctly with this style and the canvas is exported into png correctly. However when I try to do the same thing in a standalone non-GUI application the output image contains only that symbology layers that are not using SVG-images (pattern fills or marker outlines are not rendered) and I have following errors in console:

QPainter::begin: Paint device returned engine == 0, type: 3
QPainter::begin: Paint device returned engine == 0, type: 3
QPainter::setRenderHint: Painter must be active to set rendering hints
QPainter::setPen: Painter not active
QPainter::setPen: Painter not active
QPainter::end: Painter not active, aborted
QPainter::begin: Paint device returned engine == 0, type: 3

The code:

from qgis.core import * 
from PyQt4.QtCore import QSize
from PyQt4.QtGui import QPainter, QImage, QColor

qgisApp = QgsApplication([], False)
QgsApplication.setPrefixPath('/usr', True)

lyr = QgsVectorLayer('bbox.shp', 'bbox', 'ogr')
lyr_id = lyr.id()
st = lyr.applyNamedStyle('bbox.qml', 'error')

# create image
x_y = (800, 600)
img = QImage(QSize(*x_y), QImage.Format_ARGB32_Premultiplied)

# create painter
p = QPainter()

layers = QgsMapLayerRegistry.instance().mapLayers()
lst = layers.keys()
renderer = QgsMapRenderer()

extent = renderer.fullExtent()
rect = QgsRectangle(extent)

# set output size
renderer.setOutputSize(img.size(), img.logicalDpiX())

# do the rendering

# save image
img.save('qgis_render.png', 'png')


bbox.qml (14.8 KB) Yury Ryabov, 2016-05-16 08:26 AM


#1 Updated by Giovanni Manghi almost 7 years ago

  • Easy fix? set to No
  • Regression? set to No

#2 Updated by Giovanni Manghi about 5 years ago

  • Resolution set to end of life
  • Status changed from Open to Closed

End of life notice: QGIS 2.18 LTR


QGIS 3.4 has recently become our new Long Term Release (LTR) version. This is a major step in our history – a long term release version based on the massive updates, library upgrades and improvements that we carried out in the course of the 2.x to 3x upgrade cycle.

We strongly encourage all users who are currently using QGIS 2.18 LTR as their preferred QGIS release to migrate to QGIS 3.4. This new LTR version will receive regular bugfixes for at least one year. It also includes hundreds of new functions, usability improvements, bugfixes, and other goodies. See the relevant changelogs for a good sampling of all the new features that have gone into version 3.4

Most plugins have been either migrated or incorporated into the core QGIS code base.

We strongly discourage the continued use of QGIS 2.18 LTR as it is now officially unsupported, which means we’ll not provide any bug fix releases for it.

You should also note that we intend to close all bug tickets referring to the now obsolete LTR version. Original reporters will receive a notification of the ticket closure and are encouraged to check whether the issue persists in the new LTR, in which case they should reopen the ticket.

If you would like to better understand the QGIS release roadmap, check out our roadmap page! It outlines the schedule for upcoming releases and will help you plan your deployment of QGIS into an operational environment.

The development of QGIS 3.4 LTR has been made possible by the work of hundreds of volunteers, by the investments of companies, professionals, and administrations, and by continuous donations and financial support from many of you. We sincerely thank you all and encourage you to collaborate and support the project even more, for the long term improvement and sustainability of the QGIS project.

Also available in: Atom PDF