[Qgis-developer] reorganizing menus

Alexander Bruy alexander.bruy at gmail.com
Fri Dec 23 09:38:35 EST 2011

Hi Anita,

2011/12/23 Anita Graser <anitagraser at gmx.at>:
> What exactly has to be added to the plugin code now? Could you write a quick
> guide for plugin devs to follow?

Here is small example. Assume we have plugin that defines two actions: one for
launch plugin main window and another for open About dialog. And our plugin
should go to the Raster menu.

As this methods available only in QGIS 1.9.90 starting from particular commit,
first thing we should to do is check if this method available in current QGIS
version or no. If methods available then we place plugin under Raster menu in
other case we will use old Plugins menu.

So our initGui() will look as (self.iface - is a pointer to QGIS interface):

def initGui( self ):
  # define actions
  self.actionRun = QAction( QIcon( ":/icon.png" ), "Run plugin",
self.iface.mainWindow() )
  self.actionAbout = QAction( QIcon( ":/about.png" ), "About",
self.iface.mainWindow() )

  # connect signals to slots
  QObject.connect( self.actionRun, SIGNAL( "triggered()" ), self.run )
  QObject.connect( self.actionAbout, SIGNAL( "triggered()" ), self.about )

  # time to create UI (menu and buttons)
  if hasattr(self.iface, "addPluginToRasterMenu"):
    # new menu available so add both actions into PluginName submenu
    # under Raster menu
    self.iface.addPluginToRasterMenu( "PluginName", self.actionRun )
    self.iface.addPluginToRasterMenu( "PluginName", self.actionAbout )
    # and add Run button to the Raster panel
    self.iface.addRasterToolBarIcon( self.actionRun )
    # oops... old QGIS without additional menus. Place plugin under
    # Plugins menu as usual
    self.iface.addPluginToMenu( "PluginName", self.actionRun )
    self.iface.addPluginToMenu( "PluginName", self.actionAbout )
    # and add Run button to the Plugins panel
    self.iface.addToolBarIcon( self.actionRun )

Same approach used in unload():

def unload( self ):
  if hasattr(self.iface, "addPluginToRasterMenu"):
    # new menu used, remove submenus from main Raster menu
    self.iface.removePluginRasterMenu( "PluginName", self.actionRun )
    self.iface.removePluginRasterMenu( "PluginName", self.actionAbout )
    # also remove button from Raster toolbar
    self.iface.removeRasterToolBarIcon( self.actionRun )
    # Plugins menu used, remove submenu and toolbar button
    self.iface.removePluginMenu( "PluginName", self.actionRun )
    self.iface.removePluginMenu( "PluginName", self.actionAbout )
    self.iface.removeToolBarIcon( self.actionRun )

Similar methods exists for all new menus and toolbars. Here is full list.
Methods to add/remove buttons on toolbars:

# Add an icon to the Plugins toolbar
addToolBarIcon (QAction *qAction)
# Remove an action (icon) from the Plugins toolbar
removeToolBarIcon (QAction *qAction)

# Add an icon to the Raster toolbar
addRasterToolBarIcon (QAction *qAction)
# Remove an action (icon) from the Raster toolbar
removeRasterToolBarIcon (QAction *qAction)

# Add an icon to the Vector toolbar
addVectorToolBarIcon (QAction *qAction)
# Remove an action (icon) from the Vector toolbar
removeVectorToolBarIcon (QAction *qAction)

# Add an icon to the Database toolbar
addDatabaseToolBarIcon (QAction *qAction)
# Remove an action (icon) from the Vector toolbar
removeDatabaseToolBarIcon (QAction *qAction)

Methods to add/remove items to menus:

# Add action to the Plugins menu
addPluginToMenu (QString name, QAction *action)
# Remove action from the Plugins menu
removePluginMenu (QString name, QAction *action)
# Add "add layer" action to Layer menu.
insertAddLayerAction (QAction *action)
# Remove "add layer" action from Layer menu.
removeAddLayerAction (QAction *action)

# Add action to the Database menu.
addPluginToDatabaseMenu (QString name, QAction *action)
# Remove action from the Database menu.
removePluginDatabaseMenu (QString name, QAction *action)

# Add action to the Raster menu.
addPluginToRasterMenu (QString name, QAction *action)
# Remove action from the Raster menu.
removePluginRasterMenu (QString name, QAction *action)

# Add action to the Vector menu.
addPluginToVectorMenu (QString name, QAction *action)
# Remove action from the Vector menu.
removePluginVectorMenu (QString name, QAction *action)

List of this methods available in API docs, see QgsInterface
class [0]. Also soon I will add Web menu/toolbar with methods:

# Add action to the Web menu.
addPluginToWebMenu (QString name, QAction *action)
# Remove action from the Vector menu.
removePluginWebMenu (QString name, QAction *action)

# Add an icon to the Web toolbar
addWebToolBarIcon (QAction *qAction)
# Remove an action (icon) from the Web toolbar
removeWebToolBarIcon (QAction *qAction)

Hope this helps. If you still have any questions don't hesitate
to ask.

[0] http://qgis.org/api/classQgisInterface.html

Alexander Bruy

More information about the Qgis-developer mailing list