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).
Answer
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.
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.
No comments:
Post a Comment