Sunday 27 March 2016

pyqgis - Adding new menu item to QGIS Desktop app?



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.


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.


No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...