python3 super doesn't work with PyQt classes

白昼怎懂夜的黑 提交于 2019-12-05 05:29:36

Along with @nneonneo I also suspect that QtCore.QObject doesn't use the cooperative super.__init__. If it did, you wouldn't have this problem.

However, you should be aware that at some point one of the base classes cannot use cooperative super because object won't have the method. Consider:

class Base():
    def __init__(self):
        print("initializing Base")
        super().__init__()
    def helper(self, text):
        print("Base helper")
        text = super().helper(text)
        text = text.title()
        print(text)

class EndOfTheLine():
    def __init__(self):
        print("initializing EOTL")
        super().__init__()
    def helper(self, text):
        print("EOTL helper")
        text = super().helper(text)
        return reversed(text)

class FurtherDown(Base, EndOfTheLine):
    def __init__(self):
        print("initializing FD")
        super().__init__()
    def helper(self, text):
        print(super().helper(text))

test = FurtherDown()
print(test.helper('test 1 2 3... test 1 2 3'))

and the output:

initializing FD
initializing Base
initializing EOTL
Base helper
EOTL helper
Traceback (most recent call last):
  File "test.py", line 28, in <module>
    print(test.helper('test 1 2 3... test 1 2 3'))
  File "test.py", line 25, in helper
    print(super().helper(text))
  File "test.py", line 7, in helper
    text = super().helper(text)
  File "test.py", line 17, in helper
    text = super().helper(text)
AttributeError: 'super' object has no attribute 'helper'

So, whichever class is going to be the End of the Line needs to not call super. Because there are other Qt methods you might want to override, that dictates that the Qt class must be the last one in the class header. By not having __init__ use cooperative super, even though it could, Qt is avoiding bugs further down when some other method is overridden.

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