1

I am trying to place a plugin into a submenu of "Vector". In this case "Geoprocessing Tools".

When I use:

iface.addPluginToVectorMenu('Geoprocessing Tools',action) 

or:

iface.addPluginToVectorMenu('&Geoprocessing Tools',action) 

it creates a new and double submenu "Geoprocessing Tools" with the action placed only in the new one. Do I miss some formatting rules or is it just not possible?

double submenu

1 Answer 1

4

It can be improved (tests, search of the submenu text) but the code below will add your action in the right Geoprocessing Tools submenu :

def add_plugin_to_geoprocessing_menu(action: QAction) -> bool:
    vector_menu = iface.vectorMenu()
    geoprocessing_action = [
        a for a in vector_menu.findChildren(QAction) if "Geoprocessing" in a.text()
    ][0]
    geoprocessing_menu = geoprocessing_action.menu()
    geoprocessing_menu.addAction(action)
    return True


add_plugin_to_geoprocessing_menu(action)
3
  • Thank you very much. It gives an error when starting QGis : "a for a in vector_menu.findChildren(QAction) if "Geoprocessing" in a.text()][0] IndexError: list index out of range" But when reloaded it adds to the right Submenu. I guess the vector menu isn't yet loaded when this plugin loads?
    – JAS
    Commented Apr 22, 2022 at 11:39
  • I think you're right. I didn't test the code inside a plugin. QGIS should load other plugins before its submenus (related to processing core plugins), so currently, I have no idea to achieve this for your plugin context. Commented Apr 22, 2022 at 11:47
  • That is probably also the reason the submenus doubles in the first place. Again thanks, learned a lot from it :)
    – JAS
    Commented Apr 22, 2022 at 13:54

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.