2

As I get more into QGIS scripting, I refer to the documentation often and usually without success. I have trouble deciphering how to interpret the classes, etc.

I found this thread which is really the same question and it helps a bit but I am still confused.

Let's take the QgsLayoutItemLegend class for example from the documentation:

Now, I think I understand that in order to create a legend item and add it to my Print Layout, I would do something like this...

QgsLayoutItemLegend.create(legend)

In the QGIS python editor I am prompted to include arguments for the function, which is helpful. It looks like this: enter image description here

I look in the documentation for the QgsLayoutItemLegend.create() method and see this below, which I cannot make sense of: enter image description here

I see nothing about the arguments this method takes in the documentation.

How do I create a Legend using QgsLayoutItemLegend?

PolyGeo
  • 65,136
  • 29
  • 109
  • 338
Erich Purpur
  • 1,861
  • 3
  • 18
  • 35

1 Answers1

2

You create it directly, using the layout as the only argument:

legend=QgsLayoutItemLegend(layout)

Then you must add the newly created item to the layout (this isn't done automatically):

layout.addItem(legend)
ndawson
  • 27,620
  • 3
  • 61
  • 85