2

I created my plugin with Plugin Builder in QGIS. It works great. Now I want to add more functions (http://qgis4dummies.wdfiles.com/local--files/grass-plugin/01_nuovo_mapset_grass_qgis.png like in this picture - there are many functions under GRASS plugin). How can I do it? I tried to create another plugin, but I wasn't able to join them under one plugin.

6

Sometimes it's easier to look at the code of an existing plugin which has the functionality you want, multiple menu items.

You can find a link to the source on the description of each plugin in the Plugin Manager - usually to Github or a similar repository.

Unfortunately, you can't do this with the Grass plugin as it's a shared object (dll on Windows).

On my installed plugins I see that QConsolidate and Globe have multiple menu items.

The good news is that it's fairly easy, and you don't need to write multiple plugins - you just add new functions to your plugin class, and modify your initGui() method to add a new menu item, and bind it to your method.

Have a look at the QConsolidate plugin, which has two menu items, one for the main plugin, and one for an 'about' dialog, (specifically the initGui() method)

The latest version of QGIS Plugin builder adds an addAction() method that makes it easy to register a new menu item and associate it with a method. By default this will add a menu item for you.

The plugin builder will aready have created a stub initGui() for you, which should look something like this;

def initGui(self):
    """Create the menu entries and toolbar icons inside the QGIS GUI."""
    icon_path = ':/plugins/createnamedgrid/icon.png'
    self.add_action(
        icon_path,
        text=self.tr(u'Create named vector grid'),
        callback=self.run,
        parent=self.iface.mainWindow())

..you just need to add your new function and wire it in using another add_action call...

    """add this"""

    icon_path = ':/plugins/createnamedgrid/icon.png'
    self.add_action(
        icon_path,
        text=self.tr(u'Do something else'),
        callback=self.some_other_method,
        parent=self.iface.mainWindow())

redeploy, refresh, and you now have an extra menu item...

enter image description here

The tr() call is to allow for language localisation, so you can have the menu names appear in different languages.

You'll may also need to add code to the unload() method.

For more information about what these methods do, check the answers to What is the purpose of some functions and files in qgis python plugins?

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.