7

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: http://img195.imageshack.us/img195/6375/selectfeaturedropdown.png

Is this possible with Python in QGIS?

1 Answer 1

10

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)
4
  • hmm addToolBarIcon should really return a QAction so it saves having to make the action first.
    – Nathan W
    Commented Apr 28, 2013 at 5:08
  • I would really love to see an addToolBarButton method to allow finer grained control. Commented Apr 28, 2013 at 8:50
  • Well there is no API freeze yet ;)
    – Nathan W
    Commented Apr 28, 2013 at 8:51
  • 3
    Nathan W: addToolBarWidget() et.al. available now in master. Commented Apr 28, 2013 at 19:19

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.