14

Through tutorials I learned how to add a toolbutton to the plugins-toolbar via python. Now I wonder how to add a complete toolbar with toolbarbuttons via python.

Can anybody give some example-code?

PolyGeo
65.5k29 gold badges115 silver badges350 bronze badges
asked Aug 3, 2012 at 16:24

2 Answers 2

20

You can use the addToolBar() API call via QgisInterface (i.e. iface) to create a custom toolbar:

class MyPlugin:
 def __init__(self, iface):
 # Save reference to the QGIS interface
 self.iface = iface
 def initGui(self):
 # Add toolbar 
 self.toolbar = self.iface.addToolBar("My_ToolBar")
 # Create actions 
 self.someact = QAction(QIcon(":/plugins/MyPlugin/icons/someactionicon.png"),
 QCoreApplication.translate("MyPlugin", "My Action"),
 self.iface.mainWindow())
 # Connect action signals to slots
 self.someact.triggered.connect(self.doSomething)
 # Add actions to the toolbar
 self.toolbar.addAction(self.someact)
 def unload(self):
 # remove toolbar on plugin unload
 del self.toolbar
 def doSomething(self):
 # slot for action
 pass
answered Aug 3, 2012 at 18:08
1

I have posted an answer to a post of mine here:

How can I add a second Toolbar Button and Dialog to a QGIS plugin built with Plugin Builder?

that may also answer your question

answered May 29, 2016 at 12:33

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.