Max
Max

Reputation: 2162

PyQt5 custom QAction class

In an effort to reduce clutter. I am trying to produce my own class that inherits from QAction. From a QMainWindow I want to call reproduce the following code:

class MainWindow(QMainWindow):

    def __init__(self):
        super().__init__()

        exitAction = QAction(QIcon('exit.png'), '&Exit', self)        
        exitAction.setShortcut('Ctrl+Q')
        exitAction.setStatusTip('Exit application')
        exitAction.triggered.connect(self.quit)

        menubar = self.menuBar()
        fileMenu = menubar.addMenu('&File')
        fileMenu.addAction(exitAction)

As you can see I am simply adding an action to a menubar. But I would like to make my program more object oriented. I would hope the following is possible:

from PyQt5.QtWidgets import QAction
from PyQt5.QtGui import QIcon

class exitAction(QAction):

    def __init__(self,parent):
        super.__init__(QIcon('exit.png'), '&Exit', parent)
        self.setShortcut('Ctrl+Q')
        self.setStatusTip('Exit application')
        self.triggered.connect(parent.quit)

Where the exitAction class is called via the following:

class MainWindow(QMainWindow):
     def __init__(self):

        #Create Menu
        self.menuBar = self.menuBar()

        #Add File Menu
        file_menu = self.menuBar.addMenu('&File')
        file_menu.addAction(exitAction(self))

This seems simple enough but what doesn't make sense to me is why the near-equivalent code works fine on its own.

The error I get is TypeError: descriptor '__init__' requires a 'super' object but received a 'QIcon'. The problem I've set myself up with might be a python misunderstanding as well. If I were working in C++ I would simply pass a pointer that references the MainWindow.

Upvotes: 1

Views: 1107

Answers (1)

fghj
fghj

Reputation: 9404

I suppose your problem here:

super.__init__(QIcon('exit.png'), '&Exit', parent)

you should write super(). not super.

Upvotes: 2

Related Questions