1

currently i want to add a qgis toolbar icon, which needs to be populated on the qgis window after certain process using custom class by overriding the current qgisinterface. But while running the code i got no issue on the code but the toolbar icon is not populating. Anyone can look at the code for related bugs.

class CustomMenuPlugin:
      def __init__(self, iface):
         self.iface = iface

      def initGui(self):
         self.toolbar = self.iface.addToolBar("CustomMenu")
         self.add_custom_menu()

      def add_custom_menu(self):
         custom_menu = QMenu("Custom Menu", self.iface.mainWindow())
         action1 = QAction("Action 1", self.iface.mainWindow())
         action1.triggered.connect(self.custom_action_triggered)
         custom_menu.addAction(action1)

         action2 = QAction("Action 2", self.iface.mainWindow())
         action2.triggered.connect(self.custom_action_triggered)
         custom_menu.addAction(action2)

         # You can add more actions as needed

         self.custom_menu_button = QAction("Custom Menu", self.iface.mainWindow())
         self.custom_menu_button.setMenu(custom_menu)
         self.toolbar.addAction(self.custom_menu_button)

      def unload(self):
        self.iface.removeToolBarIcon(self.toolbar, self.custom_menu_button)

      def custom_action_triggered(self):
         # Implement the logic for your custom actions here
         pass

  custom_menu_plugin = CustomMenuPlugin(self.obj_iface)
4
  • Either you haven't provided enough code in your example or your initGui function never gets called. Commented Jan 12, 2024 at 8:27
  • Also if your self.obj_iface is a QgisInterface object then your unload() function will raise error TypeError: QgisInterface.removeToolBarIcon(): argument 1 has unexpected type 'QToolBar' Commented Jan 12, 2024 at 8:40
  • ok let me try it out. Commented Jan 12, 2024 at 9:00
  • Now the issue has been resolved after, passing the QgisInterface as a parameter instead a object refernce. Commented Jan 12, 2024 at 11:00

0

Your Answer

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.