0

I am trying to trigger through code the clicking of a QMenu submenu item. For example, I want to trigger "report 2 under Jack's reports" in the following QMenu/submenu:

enter image description here

The following will trigger "Combined report" in the main menu:

def view(self):
    report_menu = self.reportsWindow.ui.inhira_btn.menu()
    report_menu.actions()[0].triggered.emit()
    self.reportsWindow.show()

However, I want to trigger a submenu item (report 2 under Jack's reports). Without success, I've tried various subscriptable combinations like:

report_menu.actions()[1][1].triggered.emit()
7
  • Are those actions dynamically created? Commented May 20 at 16:58
  • Don't understand your question. Commented May 20 at 17:04
  • How do you create those menus and actions? Commented May 20 at 17:08
  • The menu is created in the init of a module named reportsWindow and is attached to a button. The function is in another module. Commented May 20 at 17:10
  • 1
    Then you could just set those menus as attributes of the menu (eg: mainMenu = QMenu() menu.subMenu = menu.addMenu('sub menu') and access them with ...menu().subMenu().actions(). Note that sub menus are added as actions, not lists. Your last code is invalid because report_menu.actions()[1] returns an action, and therefore not iterable. You must access the menu contained in the action, and then get its own actions: report_menu.actions()[1].menu().actions()[1]. Commented May 20 at 18:06

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.