I am trying to develop a standalone PyQGIS application.

The map display does not pose any problems, but I do not know how to add a legend to the canvas.

  • What do you intend to do? Display a legend (i.e., symbology for all map layers), be able to alter layer status (e.g., set visibility, set layer order, and so on), or both? – Germán Carrillo Apr 6 '15 at 13:18
  • Here's what to do my application After analysis and processing, it must display a map with graduated symbols (5 classes). it must also display the legend (value of classes). I can view the map with graduated symbols on a canvas, but I still can not find how to add the legend. If there is a code example that can help me, thank you – BenMed Apr 6 '15 at 13:47

To complement Luigi's answer, this is how you can use the Layer Tree View for adding a legend and/or a ToC to your application.

After you create your canvas in your standalone PyQGIS application, you need to create a QgsLayerTreeView. For that, you need a model, and for the model you need the layer tree root. Additionally, you need to use a QgsLayerTreeMapCanvasBridge to keep both map and tree in sync. All this is done with the following code snippet (taken from this Martin Dobias' post):

self.root = QgsProject.instance().layerTreeRoot()
self.bridge = QgsLayerTreeMapCanvasBridge(self.root, self.canvas)
self.model = QgsLayerTreeModel(self.root)
self.view = QgsLayerTreeView()

Now, you might want to embed the view into a dock widget inside your application. To do so, you can write the following code right below the previous code:

self.LegendDock = QDockWidget( "Layers", self )
self.LegendDock.setObjectName( "layers" )
self.LegendDock.setAllowedAreas( Qt.LeftDockWidgetArea | Qt.RightDockWidgetArea )
self.LegendDock.setWidget( self.view )
self.LegendDock.setContentsMargins ( 9, 9, 9, 9 )
self.addDockWidget( Qt.LeftDockWidgetArea, self.LegendDock )

When you run your application and load layers or alter any layer symbology, you should see changes reflected in your dock widget, this way:

enter image description here

I followed @kelly-thomas's first example from How to apply a graduated renderer in PyQGIS? for graduated symbols on Natural Earth Data.

Besides displaying your layers' symbology, the Layer Tree View allows you to rename layers, set layer visibility, and change layer order in the map canvas.

Note: Make sure you import the required modules from qgis.gui, PyQt4.QtCore, and PyQt4.QtGui (or simply do from xxxx import * for all three).

  • I have successfully added the legend in my standalone Application. Thanks! – BenMed Apr 7 '15 at 9:47
  • That's great! You're welcome. – Germán Carrillo Apr 7 '15 at 12:15
  • 1
    I was going to answer this the other day then I though 'nah @gcarrillo will get it :P' – Nathan W Apr 8 '15 at 4:54
  • I strongly suggest to create styling in a different way: A) The simplest way, if you don't need to load layers or legend dinamically follow kartoza.com/how-to-load-a-qgis-project-in-python B) or create a style in QGIS Desctop on a similar layer and apply it using the method QgsVectorLayer.loadNamedStyle(...) – Luigi Pirelli Apr 8 '15 at 8:56
  • I think that it is a good idea to separate both approaches for more clarity. In the second approach, is it possible to create dynamically "a style" directly in the application ? – BenMed Apr 8 '15 at 9:50

Follow this guide Lutra consulting qgis-layer-tree-api-part-3

Not the answer you're looking for? Browse other questions tagged or ask your own question.