Bug report #10945

python API QgsComposerLegend regression 2.2 -> 2.4

Added by Pierre PM almost 10 years ago. Updated almost 10 years ago.

Status:Closed
Priority:Severe/Regression
Assignee:-
Category:Python plugins
Affected QGIS version:2.4.0 Regression?:No
Operating System: Easy fix?:No
Pull Request or Patch supplied:No Resolution:invalid
Crashes QGIS or corrupts data:No Copied to github as #:19298

Description

The following code works in QGIS 2.2 :

mapRenderer= self.iface.mapCanvas().mapRenderer()
c= QgsComposition(mapRenderer)
legend= QgsComposerLegend(c)
legend.setTitle(u"TITLE")
legend.model().setLayerSet(mapRenderer.layerSet())
legend.setItemPosition(20, 195, 10, 10)
c.addItem(legend)

In QGIS 2.4, I can see on the rendered map the classification names but not the rectangles of styles, it's just blank.

Thanks,
Pierre

History

#1 Updated by Giovanni Manghi almost 10 years ago

  • Priority changed from Normal to Severe/Regression
  • Category set to Python plugins

#2 Updated by Nyall Dawson almost 10 years ago

  • Status changed from Open to Feedback

Works OK for me... can you test with:

mapSettings = iface.mapCanvas().mapSettings()
c= QgsComposition(mapSettings)

instead?

Otherwise, are you able to post a complete code sample which demonstrates this issue. For reference, here's the code I used to test with:

from PyQt4.QtGui import *
from PyQt4.QtCore import *

mapRenderer = iface.mapCanvas().mapRenderer()
c=QgsComposition(mapRenderer)
legend=QgsComposerLegend(c)
legend.setTitle(u"TITLE")
legend.model().setLayerSet(mapRenderer.layerSet())
legend.setItemPosition( 0, 10, 100, 100 )
c.addItem(legend)

printer = QPrinter()
printer.setOutputFormat(QPrinter.PdfFormat)
printer.setOutputFileName("d:\\out.pdf")
printer.setPaperSize(QSizeF(c.paperWidth(), c.paperHeight()), QPrinter.Millimeter)
printer.setFullPage(True)
printer.setColorMode(QPrinter.Color)
printer.setResolution(c.printResolution())
pdfPainter = QPainter(printer)
paperRectMM = printer.pageRect(QPrinter.Millimeter)
paperRectPixel = printer.pageRect(QPrinter.DevicePixel)

c.render(pdfPainter, paperRectPixel, paperRectMM)
pdfPainter.end()

#3 Updated by Pierre PM almost 10 years ago

OK, I guess that in QGIS 2.2, the 2 last arguments of legend.setItemPosition() didn't matter, but in 2.4 if they are too small you won't see the colored rectangles but you will see the text of the legend (using your code and just replacing 0, 10, 100, 100 with 0, 10, 10, 10).
It's OK, I just need to put some large numbers for these 2 last arguments; this behavior is strange though.

#4 Updated by Nyall Dawson almost 10 years ago

  • Resolution set to invalid
  • Status changed from Feedback to Closed

Also available in: Atom PDF