[英]PyQt5 custom QAction class
In an effort to reduce clutter. 为了减少混乱。 I am trying to produce my own class that inherits from
QAction
. 我试图产生自己的类,该类继承自
QAction
。 From a QMainWindow
I want to call reproduce the following code: 我想从
QMainWindow
调用以重现以下代码:
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: 通过以下方式调用
exitAction
类:
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'
. 我得到的错误是
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. 我自己设置的问题也可能是python的误解。 If I were working in C++ I would simply pass a pointer that references the MainWindow.
如果我使用的是C ++,则只需传递一个引用MainWindow的指针。
I suppose your problem here: 我想你的问题在这里:
super.__init__(QIcon('exit.png'), '&Exit', parent)
you should write super().
您应该编写
super().
not super.
不
super.
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.