pythonpython-3.xpyqtpyqt5qmenubar

QAction not triggered for added QMenu


The issue that I'm facing is when I want to split the functionality of the menubar into multiple files (classes), each of them specific for handling options (File/Help/Edit and so on).

In the Main UI class I have:

class MyFrame(QMainWindow):

    def __init__(self):
        super().__init__()
        self.menu_bar = self.menuBar()
        # Create menu
        self.add_menu()

    def add_menu(self):
        help_menu = MenuHelp(self)


    def getMenuBar(self):
        return self.menu_bar

In the MenuHelp (class):

class MenuHelp(QMenu):

    def __init__(self, parrent_widget):
        super(MenuHelp, self).__init__()
        self.menu_variable = parrent_widget.getMenuBar().addMenu('Help')
        about_action = self.menu_variable.addAction('About')
        about_action.setStatusTip('About')
        about_action.triggered.connect(self.handle_trigger)

    def handle_trigger(self):
        print('Im here')

The menubar is correctly shown, but handle_trigger method is never called, any ideas on what am I doing wrong?


Solution

  • You must pass a parent to your QMenu. You must change:

    class MenuHelp(QMenu):
        def __init__(self, parrent_widget):
            super(MenuHelp, self).__init__()
    

    to:

    class MenuHelp(QMenu):
        def __init__(self, parrent_widget):
            super(MenuHelp, self).__init__(parrent_widget)