从QWidget和另一个基类继承吗? [英] Multiply inherit from QWidget and another base class?

查看:282
本文介绍了从QWidget和另一个基类继承吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图创建一个PyQt5.QtWidgets.QWidget派生的小部件,同时从另一个基类Base继承.

I am trying to create a PyQt5.QtWidgets.QWidget derived widget, whilst at the same time inherit from another base class, Base.

Base继承会在调用QWidget构造函数时导致错误,说我从未提供Base所需的参数.

Inheriting from Base causes an error when calling the QWidget constructor, saying I never provided the arguments which Base requires.

这是我尝试调用QWidgetBase构造函数的方式:

This is how I attempt to call the QWidget and Base constructors:

class Deriv(QtWidgets.QWidget, Base):
    def __init__(self):
        QtWidgets.QWidget.__init__(self)
        Base.__init__(self, "hello world")

我得到的错误是:

    QtWidgets.QWidget.__init__(self)
TypeError: __init__() missing 1 required positional argument: 'id'

是否可以同时从QWidgetBase继承,如果可以的话,如何正确调用它们各自的构造函数?

Is it possible to inherit from both QWidget and Base, and if so, how do I call their respective constructors correctly?

这是一个示例应用程序,可重现我遇到的错误:

Here is an exemplar app which reproduces the error I am experiencing:

#!/usr/bin/env python3
import sys
from PyQt5 import QtWidgets

class Base(object):
    def __init__(self, id):
        self.id = id

class Deriv(QtWidgets.QWidget, Base):
    def __init__(self):
        QtWidgets.QWidget.__init__(self)
        Base.__init__(self, "hello world")

        self.show()

def main():
    app = QtWidgets.QApplication(sys.argv)
    d = Deriv()
    sys.exit(app.exec_())

if __name__ == '__main__':
    main()

这是回溯:

Traceback (most recent call last):
  File "./test.py", line 22, in <module>
    main()
  File "./test.py", line 18, in main
    d = Deriv()
  File "./test.py", line 11, in __init__
    QtWidgets.QWidget.__init__(self)
TypeError: __init__() missing 1 required positional argument: 'id'

请注意,如果我仅继承自QWidget (因此只需删除所有引用Base的内容),那么代码就可以工作

Note that if I only inherit from QWidget (so just remove everything referring to Base), then the code works

class Deriv(QtWidgets.QWidget):
    def __init__(self):
        QtWidgets.QWidget.__init__(self)

        self.show()

推荐答案

我假设QtWidgets.QWidget.__init__本身使用super来调用__init__.对于您的单个继承案例,MRO由三个类组成:[Deriv, QtWidgets.QWidget, object].调用super(QtWidgets.QWidget, self).__init__会调用object.__init__,它不需要任何其他参数.

I'm assuming QtWidgets.QWidget.__init__ itself uses super to call __init__. With your single inheritance case, the MRO consists of three classes: [Deriv, QtWidgets.QWidget, object]. The call super(QtWidgets.QWidget, self).__init__ would call object.__init__, which doesn't expect any additional arguments.

在您的多继承示例中,MRO看起来像

In your multiple inheritance example, the MRO looks like

[Deriv, QtWidgets.QWidget, Base, object]

这意味着 now 调用super(QtWidgets.QWidget, self).__init__并不引用object.__init__,而是Base.__init__确实需要一个附加参数,从而导致您看到错误

which means now the call super(QtWidgets.QWidget, self).__init__ refers not to object.__init__, but Base.__init__, which does require an additional argument, leading to the error you see.

这篇关于从QWidget和另一个基类继承吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆