I have successfully added legends in Map using Qgis QgsComposerLegend () python programming .(using Qgis python console) but legends are added vertically.I wanted to place legend horizontally. If you guys have any idea of adding them horizontally,please share with me.

  • 1
    Did you try to apply the setColumnCount( int ) method to your legend? – ArMoraer Mar 2 '16 at 17:05
  • Don't know how to do it in pyqgis. But in map composer that is done by setting the number of columns at least as big as the number of legend items. So @ArMoraer must be on the right track. – Alexandre Neto Mar 15 '16 at 9:36

I had the same problem, but searching through the QgsComposerLegend Class i solved it. Try using setSplitLayer(True) and setColumnCount(len(layers)) where 'layers' are equal to QgsMapLayerRegistry.instance().mapLayers().values(). An example:

#... Once composer is created, add a legend as follows
layers = QgsMapLayerRegistry.instance().mapLayers().values()
legend = QgsComposerLegend(c)
legend.model().setLayerSet([layer.id() for layer in layers])
legendSize = legend.paintAndDetermineSize(None)
# Define xposition and yposition
legend.setItemPosition(xpos, ypos, legendSize.width(), legendSize.height())
## this is the part where you set up the horizontal legend
# Set the column count as the number of layers you are working with
# Set split to true to finish legend horizontal distribution

enter image description here

I'm using QGIS 2.18 Las Palmas.

| improve this answer | |

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.