4

I have created a few toolbars all of them have their own menu item in the main menu of QGIS. Now I want to have them as sub-menus in one menu item, I managed to check if my menu item exists like so.

self.menu = self.iface.mainWindow().findChild( QMenu, '&MyMenu' )

# If the menu does not exist, create it!
if not self.menu:
   self.menu = QMenu( '&MyMenu', self.iface.mainWindow().menuBar() )
   self.menu.setObjectName( '&MyMenu' )
   actions = self.iface.mainWindow().menuBar().actions()
   lastAction = actions[-1]
   self.iface.mainWindow().menuBar().insertMenu( lastAction, self.menu )

But now when the menu exists already i need to check if a sub-menu already exists of if i have to create it. I tried something like this but it still create the submenu again if the plugin is reloaded.

self.menuStrasse = self.menu.findChild(QMenu, '&MySubMenu')

if not self.menuStrasse:
   self.menuStrasse = self.menu.addMenu( "MySubMenu")

self.menuStrasse.addActions( [self.action1,self.action2])

Can someone point me in the right direction?

  • self.menuStrasse = self.menu.addMenu( "MySubMenu") That is how i add the submenu, but I edited the question to show how i add the actions to the submenu. The other submenus are added the same way in there respective initGui(self) method – Adarkas2302 May 24 '17 at 9:25
3

Instead of checking if the submenu exists, you could just check if the main menu exists (which you have done) and if it doesn't, add the menu and the submenus at the same time:

self.menu = self.iface.mainWindow().findChild( QMenu, '&MyMenu' )

# If the menu does not exist, create it!
if not self.menu:
    self.menu = QMenu( '&MyMenu', self.iface.mainWindow().menuBar() )
    self.menu.setObjectName( '&MyMenu' )
    actions = self.iface.mainWindow().menuBar().actions()
    lastAction = actions[-1]
    self.iface.mainWindow().menuBar().insertMenu( lastAction, self.menu )

    self.menuStrasse = self.menu.addMenu( "MySubMenu")
    self.menuStrasse.addActions( [self.action1,self.action2])

This should avoid creating duplicate submenus.


Edit:

An alternative could be to check all submenu actions in the main menu by storing them in a list. If 'MySubMenu' does not exist in the list, it will be created:

self.menu = self.iface.mainWindow().findChild( QMenu, '&MyMenu' )

# If the menu does not exist, create it!
if not self.menu:
    self.menu = QMenu( '&MyMenu', self.iface.mainWindow().menuBar() )
    self.menu.setObjectName( '&MyMenu' )
    actions = self.iface.mainWindow().menuBar().actions()
    lastAction = actions[-1]
    self.iface.mainWindow().menuBar().insertMenu( lastAction, self.menu )

# Create list containing submenu actions from main menu
submenus = []
for item in self.menu.actions():
    submenus.append(item.text())

# Check submenu action list to see if 'MySubMenu' exists
submenu_list = [x for x in submenus if x == 'MySubMenu']
# If 'MySubMenu' is not in above list (i.e. does not exist), create it
if not submenu_list:
    self.menuStrasse = self.menu.addMenu( 'MySubMenu')
  • But then only one submenu would be created, the one from the first plugin started. Because in the initGui of the other plugins the menu would be there already, so the plugin would not create its own submenu. – Adarkas2302 May 24 '17 at 11:51
  • @Adarkas2302 - Edited my post to include another alternative. I'm hoping others could provide a more direct solution as I don't have any experience connecting one plugin to another :) – Joseph May 25 '17 at 13:07
  • 1
    Sorry i just now checked your edited answer and is does exactly what i need. Thanks man. – Adarkas2302 Jun 12 '17 at 11:10
  • @Adarkas2302 - Most welcome buddy, glad it helped! I just noticed you posted an answer but then deleted it, might I suggest you undelete it so that if the method I suggested does not work for someone else, they could try yours :) – Joseph Jun 12 '17 at 11:15
  • Well funny thing is I came to the same answer as you. But i only saw yours when i thought i could post my solution for others to use. So i accepted yours and deleted mine ;) – Adarkas2302 Jun 12 '17 at 13:00

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.