4

I'm trying to create a sub-menu in a QGIS plugin like in the image. I found this Add extra item to drop down menu plugin. "Layers menu from project" plugin but I'm using QGIS 3 and PyQT5 and I don't know how to adapt it. This is my code:

def initGui(self):
  icon = os.path.join(os.path.join(cmd_folder, 'logo.png'))
  self.action = QAction(QIcon(icon), 'Select Feature', self.iface.mainWindow())
  self.action.triggered.connect(self.run)
  self.iface.addPluginToMenu('&Select Feature', self.action)
  self.iface.addToolBarIcon(self.action)
  self.first_start = True

def unload(self):
  self.iface.removeToolBarIcon(self.action)
  self.iface.removePluginMenu('&Select Feature', self.action)  
  del self.action

EDIT: I solved in this way, but I'm wondering if there's a better way

def initGui(self):
  icon = os.path.join(os.path.join(cmd_folder, 'logo.png'))
  self.action = QAction(QIcon(icon), 'Select Feature', self.iface.mainWindow())
  self.action2 = QAction(QIcon(icon), 'Select Feature2', self.iface.mainWindow())
  self.action.triggered.connect(self.run)
  self.action.triggered.connect(self.run2)
  self.iface.addPluginToMenu('&Select Feature', self.action)
  self.iface.addPluginToMenu('&Select Feature', self.action2)
  self.iface.addToolBarIcon(self.action)
  self.first_start = True

def unload(self):
  self.iface.removeToolBarIcon(self.action)
  self.iface.removePluginMenu('&Select Feature', self.action) 
  self.iface.removePluginMenu('&Select Feature2', self.action2) 
  del self.action
  del self.action2

enter image description here

4
  • check this gis.stackexchange.com/q/232859/49538 and your screenshot isn't from QGIS 3.
    – Fran Raga
    Dec 1 '19 at 11:44
  • Thanks, I know that isn't from QGIS 3 but I want the same menu
    – Lorenzo
    Dec 1 '19 at 12:49
  • 2
    I think you should post your solution as a proper answer. That's how I achieve the same goal in a plugin of mine (see code snippet). You have one thing to adjust: In the last call to removePluginMenu you should use "&Select Feature" (without the '2'). That's all. And always remember, one great way to learn PyQGIS is looking at how other plugin authors did it. So, next time, pick a plugin that does what you want, examine its code and adapt it to yours. That's how programming works :) Dec 1 '19 at 22:47
  • Thanks for your advice, I'll sure do it :)
    – Lorenzo
    Dec 2 '19 at 18:00
4

This is code is a working answer:

def initGui(self):
  icon = os.path.join(os.path.join(cmd_folder, 'logo.png'))
  self.action = QAction(QIcon(icon), 'Select Feature', self.iface.mainWindow())
  self.action2 = QAction(QIcon(icon), 'Select Feature2', self.iface.mainWindow())
  self.action.triggered.connect(self.run)
  self.action.triggered.connect(self.run2)
  self.iface.addPluginToMenu('&Select Feature', self.action)
  self.iface.addPluginToMenu('&Select Feature', self.action2)
  self.iface.addToolBarIcon(self.action)
  self.first_start = True

def unload(self):
  self.iface.removeToolBarIcon(self.action)
  self.iface.removePluginMenu('&Select Feature', self.action) 
  self.iface.removePluginMenu('&Select Feature', self.action2) 
  del self.action
  del self.action2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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