I am implementing a python based plugin for use with QGIS 1.8.

Most of it's user interface is contained within a QDockWidget but there are some layer specific controls I would like to append to the context menu displayed when the user right-clicks on the Table Of Contents.

Is there a way for me to manipulate this menu?


Applies to the current development version or the upcoming release 2.0.

You can use QgsLegendInterface.addLegendLayerAction for this purpose. This method lets you add new menu entries for a LayerType (VectorLayer, RasterLayer, PluginLayer)

a = QAction( u"My Action", iface.legendInterface() )
iface.legendInterface().addLegendLayerAction( a, u"My Plugin Menu", u"id1", QgsMapLayer.VectorLayer, True )

If you set the allLayers parameter of addLegendLayerAction to False you can add the action only for specific layers. Given a variable myLayer pointing to a QgsMapLayer:

aLyr = QAction( u"My Layer Action", iface.legendInterface() )
iface.legendInterface().addLegendLayerAction( aLyr, u"My Plugin Menu", u"id2", QgsMapLayer.VectorLayer, False )
iface.legendInterface().addLegendLayerActionForLayer( aLyr, myLayer )

You would then normally connect the QAction.triggered signal to a slot with the code you want to execute when your menu entry is clicked.

You should remove the action by calling the appropriate removeLegendLayerAction when your plugin is exiting.

iface.legendInterface().removeLegendLayerAction( a )
iface.legendInterface().removeLegendLayerAction( aLyr )

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.