6

When building a plugin using the QGIS Plugin Builder plugin, if I select 'Tool button with dialog' as the template, then my plugin button will be placed in the Plugins toolbar in QGIS.

If I build the plugin boilerplate with the 'Tool button with dock widget' template, then the plugin icon is put in its own toolbar.

enter image description here

I have compared the code and cannot find how to change this. I would like to build a plugin with a dialog, but have the icon in its own toolbar.

Please let me know if there is a way to make my question more clear.

enter image description here

1 Answer 1

10

Using Plugin builder version 3.2.1 I found differences. In case of Tool button with dialog in add_action function addToolBarIcon is used (line 148):

self.iface.addToolBarIcon(action)

which "Add an icon to the plugins toolbar." (quote from QGIS API documentation).

In case of Tool button with dock widget a new toolbar is created in __init__ function (line 68):

self.toolbar = self.iface.addToolBar(u'test_dock')
self.toolbar.setObjectName(u'test_dock')

and in the add_action function the action is added to the toolbar of the plugin (line 154):

self.toolbar.addAction(action)

So if you would like to create a Tool button with dialog plug-in with its own toolbar, please add your own toolbar to QGIS GUI in the __init__ function of your plug-in, for example after self.menu = ... (line 64)

self.toolbar = self.iface.addToolBar(u'my_plugin')
self.toolbar.setObjectName(u'my_plugin')

Please replace my_plugin to the name of your plug-in.

Change the self.iface.addToolBarIcon call in add_action function (line 147) to

   if add_to_toolbar:
        self.toolbar.addAction(action)

I used the line numbers of the original generated code.

1
  • Great, thankyou for your clear help Zoltan. I had found the first part self.toolbar = self.iface.addToolBar(u'my_plugin') self.toolbar.setObjectName(u'my_plugin') but had missed the line self.toolbar.addAction(action). In the Tool button with dialog template that line is self.iface.addToolBarIcon(action), which must add it to the Plugins toolbar. Thanks again very clear answer.
    – staf
    Commented Apr 13, 2020 at 23:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.