Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

I Have a set of some QAction in my QGIS-Plugin and they are created and added to the toolbar like this

icon = QIcon(":/action1.png")
self.action1 = QAction(icon, u"Action 1", self.iface.mainWindow())
QObject.connect(self.action1, SIGNAL("triggered()"), self.someMethod)
self.iface.addToolBarIcon(self.action1)
self.iface.addPluginToMenu(u"&My Plugin", self.action1)

But now I'd like to group my Actions like in this "Select Feature" Screenshot:

Is this possible with Python in QGIS?

share|improve this question

1 Answer 1

up vote 7 down vote accepted

Up to QGIS 1.8, you can not do quite the same thing, because you can only add QActions to the QToolBar, but no QToolButtons ( which would let you set the PopupMode manually)

But you can get almost the same result if you assign a QMenu to your action, and to this menu you can add other actions. I did not find a possibility to change the PopupMode for this to anything else than MenuButtonPopup.

self.action1 = QAction(QIcon(":/action1.png"), u"Action 1", self.iface.mainWindow())
self.action2 = QAction(QIcon(":/action2.png"), u"Action 2", self.iface.mainWindow())
self.action3 = QAction(QIcon(":/action3.png"), u"Action 3", self.iface.mainWindow())

self.popupMenu = QMenu( self.iface.mainWindow() )
self.popupMenu.addAction( self.action2 )
self.popupMenu.addAction( self.action3 )

self.action1.triggered.connect( self.someMethod1 )
self.action2.triggered.connect( self.someMethod2 )
self.action3.triggered.connect( self.someMethod3 )

self.action1.setMenu( self.popupMenu )

self.iface.addToolBarIcon(self.action1)

QToolButton

Starting from QGIS 2.0 (or current development versions) there is also iface.addToolBarWidget which lets you add a QToolButton which allows finer grained control over dropdown behavior and appearance.

self.action1 = QAction(QIcon(":/action1.png"), u"Action 1", self.iface.mainWindow())
self.action2 = QAction(QIcon(":/action2.png"), u"Action 2", self.iface.mainWindow())
self.action3 = QAction(QIcon(":/action3.png"), u"Action 3", self.iface.mainWindow())

self.popupMenu = QMenu( self.iface.mainWindow() )
self.popupMenu.addAction( self.action1 )
self.popupMenu.addAction( self.action2 )
self.popupMenu.addAction( self.action3 )

self.action1.triggered.connect( self.someMethod1 )
self.action2.triggered.connect( self.someMethod2 )
self.action3.triggered.connect( self.someMethod3 )

self.toolButton = QToolButton()

self.toolButton.setMenu( self.popupMenu )
self.toolButton.setDefaultAction( self.action1 )
self.toolButton.setPopupMode( QToolButton.InstantPopup )

self.iface.addToolBarWidget( self.toolButton )
share|improve this answer
    
hmm addToolBarIcon should really return a QAction so it saves having to make the action first. –  Nathan W Apr 28 '13 at 5:08
    
I would really love to see an addToolBarButton method to allow finer grained control. –  Matthias Kuhn Apr 28 '13 at 8:50
    
Well there is no API freeze yet ;) –  Nathan W Apr 28 '13 at 8:51
    
Thank you Matthias, exactly what I was searching for! –  baal Apr 28 '13 at 11:23
3  
Nathan W: addToolBarWidget() et.al. available now in master. –  Matthias Kuhn Apr 28 '13 at 19:19

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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