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
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 :)