Python: RuntimeError: super-class __init__() of %S was never called

拟墨画扇 提交于 2019-11-28 00:55:56

If you want to inherit QObject (or QWidget), you must always call the super-class __init__:

class MyObject(QObject):
    def __init__(self, *args, **kwargs):
        super(MyObject, self).__init__(arguments to parent class)
        #other stuff here

You may also call the parent's class's __init__ after some instructions, but you can't call QObject methods or use QObject attributes until you did so.


Edit: In your case you are trying to deepcopy a QWidget, but this is not possible. Python may be able to copy the wrapper of the QWidget, but the QWidget itself is a C++ object that python cannot handle with the default implementation of copy.deepcopy, hence whenever you call a method of the copied instance you get the RuntimeError because the underlying C++ object wasn't initialized properly.

The same is true for pickling these objects. Python is able to pickle the wrapper, not the C++ object itself, hence when unpickling the instance the result is a corrupted instance.

In order to support deepcopy() the QWidget class should implement the __deepcopy__ method, but it does not do that.

If you want to copy widgets you'll have to implement by yourself all the mechanism by hand.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!