3

I have set some actions for my plugin and now I want to group all of them in a dropdown menu as one of the image, which is from another plugin. I've been struggling but I can't find a way to do it like that.

enter image description here

I have tried this:

    def initGui(self):
        """Create the menu entries and toolbar icons inside the QGIS GUI."""

        icon_path = ':/plugins/udt_plugin/icon.png'
        self.action1 = QAction(QIcon(icon_path), u"Action 1", self.iface.mainWindow())
        self.action2 = QAction(QIcon(icon_path), u"Action 2", self.iface.mainWindow())
        self.action3 = QAction(QIcon(icon_path), 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.toolButton = QToolButton()

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

        self.iface.addToolBarWidget(self.toolButton) 

But it creates a more simple dropdown menu like below, which is not exactly what I want. I'm using QGIS 3.10.

enter image description here

3
  • What do you mean by "simple"? same icons, down arrow? Apr 14, 2021 at 10:42
  • Yes, I want that my plugin button has a down arrow which opens a dropdown menu, like in the first image. Apr 14, 2021 at 11:01
  • Change the icon to a down arrow :-)
    – wingnut
    Apr 14, 2021 at 11:13

1 Answer 1

6

Change

self.toolButton.setPopupMode(QToolButton.InstantPopup)

to

self.toolButton.setPopupMode(QToolButton.MenuButtonPopup)

enter image description here

0

Your Answer

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

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