Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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
up vote 8 down vote accepted

QToolButton

Starting from QGIS 2.0 there is iface.addToolBarWidget which lets you add a QToolButton which allows fine 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 )

QGIS <= 1.8

Up to QGIS 1.8, you could not do quite the same thing, because you could 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)
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.