[GIS] How to group the plugins in QGIS menu and toolbar

pyqgisqgisqgis-pluginstoolbar

I work on and test more QGIS plugins which falls into the same category (lets call it MyProcessing). Though they fall into the same category I want to keep them as separate plugins. I would like to have these plugins in the Plugins menu sharing same menu item (My Processing) and especially sharing the same toolbar (separate from standard plugin toolbar). Is there a way to do that?

In one plugin I create toolbar in interface:

self.toolbar = self.iface.addToolBar(u'MyProcessing')
self.toolbar.setObjectName(u'MyProcessing')

How do I check in other plugin if toolbar MyProcessing exists and add icon into it?

Same with the menu item, is there a way to check if menu item already exists and add other plugin action into it?

Best Answer

Use the findChild method of QGIS mainWindow to know if there is a QToolBar called u'MyProcessing' and get it. If you don't get anything, create it.

myToolBar = iface.mainWindow().findChild( QToolBar, u'MyProcessing' )
if not myToolBar:
    myToolBar = iface.addToolBar( u'MyProcessing' )
    myToolBar.setObjectName( u'MyProcessing' )

This needs to be checked in each of your plugins. You can then use myToolBar to add actions and perhaps a separator to visually group actions (buttons) for each plugin:

myToolBar.addAction( 'A' )
myToolBar.addAction( 'B' )
myToolBar.addSeparator()
myToolBar.addAction( 'C' )

As you pointed out, reusing menus is explained here: Adding multiple plugins to custom menu in QGIS