14

I need to add new menu item to the top level menu of QGIS Desktop (2.7.0); I.e. it shall be on same level as "Project", "Edit", "View", "Layer" etc.

As far as I know there is pretty nice way how hide menu items (Settings->Customization) Plus using QgisInterface (http://qgis.org/api/classQgisInterface.html) I can add new items to Menus already defined in QGIS (addLayerMenu, addPluginToDatabaseMenu etc) from my plugin code.

But I need new Menu item on top layer (this is req from customer).

22

You can add a custom menu to the QGIS GUI this way:

self.menu = QMenu( "&My tools", self.iface.mainWindow().menuBar() )
actions = self.iface.mainWindow().menuBar().actions()
lastAction = actions[-1]
self.iface.mainWindow().menuBar().insertMenu( lastAction, self.menu )

As you can see in the code snippet above, you are adding a menu to the second to last position of the menu bar, right before the Help menu.

enter image description here

Then, you can add an action to your newly added menu this way:

self.menu.addAction( self.action )

You may already know, but just to make it clear, such GUI configuration should normally be located in the initGui() method of your plugin.

  • 1
    Nice answer (everyone loves screenshots!). Just don't forget to import QMenu at the beginning... – mozzbozz Jul 31 '17 at 14:57

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.