pyqt5 and multiple inheritance

后端 未结 1 1735
遥遥无期
遥遥无期 2020-12-19 19:08

I\'d like to create a new class that inherits two subclasses of QWidget. I know multi-inheritance isn\'t possible in pyqt, but how could I manage to have the properties of b

相关标签:
1条回答
  • 2020-12-19 19:43

    I finally found how to do it: first of all, the problems came from A and QStatusBar inheriting QWidget. We can't change QStatusBar, so we must changer A. A shouldn't inherit QWidget: so let's create another class, AInterface, like that:

    class AInterface(QObject):
        def __init__(self, a, parent=None)
            super().__init__(parent=parent)
            self.a = a
            self.connect_signal()
    
        def connect_signal(self, widget):
            widget.destroyed.connect(self.handler)
    
        @pyqtSlot()
        def handler(self):
            self.a.handler()
    

    A has now the following implementation:

    class A:
        def __init__(self, widget):
            a.widget = widget
            a.interface = AInterface(self)
    
        def handler(self):
            pass
    

    Thus, now we can create subclasses inheriting not only A but also any QObject, like this:

    class B(QStatusBar, A):
        def __init__(self, widget, parent=None):
            QStatusBar.__init__(self, parent=parent, wiget=widget)
            A.__init__(self, widget)
    
        def handler(self):
            self.show('Destroyed', 3000)
    

    Notice the widget=widget in the constructor of QStatusBar: if we don't specify it, a TypeError is thrown...

    0 讨论(0)
提交回复
热议问题