1

I'm writing a standalone app with PyQGIS to export a pdf representing a building site. I managed to write the whole code to do it, but when I'm building my pdf, I want to set the position of my QgsComposerLegend object to the LowerRight angle of the QgsComposerMap object. The position of the QgsComposerLegend item has to be set progammatically because the layer set, and in fact its size, is variable (the program will be used for a couple of qgis projects...), so I need to set this postition using the lower right corner of the composerlegend object.

I found a way to do it:

    legend = QgsComposerLegend(c)
    legend.setTitle("Legende:")
    legend.model().setLayerSet([layer.id() for layer in layers])
    legend.updateLegend()
    legendSize = legend.paintAndDetermineSize(QPainter())
    legend.setItemPosition(x+w-legendSize.width(), y+h-legendSize.height(), legendSize.width(), legendSize.height(), QgsComposerItem.UpperLeft, False, -1)
    legend.setBackgroundColor(DEFAULT_BACKGROUND_COLOR)
    legend.setBackgroundEnabled(1)
    legend.setFrameOutlineWidth(0.5)
    legend.setFrameOutlineColor(DEFAULT_COLOR)
    legend.setFrameEnabled(1)
    c.addComposerLegend(legend)

In this code: layers is the list of the layers in the composermap, x y w and h are the coordinates (in mm) of the composermap for the pdf.

Here is the problem I get: when running my code, I have these messages and I don't want them to appear:

This looks to come from the "paintAndDetermineSize" line

I would like to try something like this:

    # On met en place la legende:
    legend = QgsComposerLegend(c)
    legend.setTitle("Legende:")
    legend.model().setLayerSet([layer.id() for layer in layers])
    legend.updateLegend()
    legend.setItemPosition(newX, newY, newW, newH, QgsComposerItem.LowerRight, False, -1)
    legend.setBackgroundColor(DEFAULT_BACKGROUND_COLOR)
    legend.setBackgroundEnabled(1)
    legend.setFrameOutlineWidth(0.5)
    legend.setFrameOutlineColor(DEFAULT_COLOR)
    legend.setFrameEnabled(1)
    c.addComposerLegend(legend)

But I can't manage to make it work... Could anybody help me ?

  • Where do you created a painter?What values have setItemPosition? – Fran Raga Aug 8 '16 at 9:14
  • I didn't create any painter (later I do to set the pdf): I instanciate it in this line, this is the only way I found to get the legend's width and height... And the values are: x=25, y=20, w=249, h=162 – poulpi91 Aug 8 '16 at 9:19
  • @poulpi91 - Could you please post your edit as an answer instead? :) – Joseph Aug 8 '16 at 12:30
  • Sorry ! I'm doing this ! @Joseph – poulpi91 Aug 8 '16 at 12:31
  • @poulpi91 - No need to apologise =) – Joseph Aug 8 '16 at 12:37
2

I managed to make these messages disappear: I just tried the following lines and it worked(check the 5th line)... I still hope it won't cause any problem in my programm:

    legend = QgsComposerLegend(c)
    legend.setTitle("Legende:")
    legend.model().setLayerSet([layer.id() for layer in layers])
    legend.updateLegend()
    legendSize = legend.paintAndDetermineSize(None)
    legend.setItemPosition(x+w-legendSize.width(), y+h-legendSize.height(), legendSize.width(), legendSize.height(), QgsComposerItem.UpperLeft, False, -1)
    legend.setBackgroundColor(DEFAULT_BACKGROUND_COLOR)
    legend.setBackgroundEnabled(1)
    legend.setFrameOutlineWidth(0.5)
    legend.setFrameOutlineColor(DEFAULT_COLOR)
    legend.setFrameEnabled(1)
    c.addComposerLegend(legend)

I think the problem is solved :)

To give you an idea of what I'm working on, check this out:

enter image description here

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.