PySide Multiple Inheritance: Inheriting a QWidget and a Mixin

前端 未结 1 1833
我在风中等你
我在风中等你 2021-01-26 21:21

I\'m trying to create a set of PySide classes that inherit QWidget, QMainWindow, and QDialog. Also, I would like to inherit another class to overrides a few functions, and also

1条回答
  •  闹比i
    闹比i (楼主)
    2021-01-26 21:37

    Keep class Widget(Mixin, QtGui.Widget):, but add a super call in Mixin.__init__. This should ensure the __init__ method of both Mixin and QWidget are called, and that the Mixin implementation of the setLayout method is found first in the MRO for Widget.

    class Mixin(object):
        def __init__(self, parent=None, arg=None):
            super(Mixin, self).__init__(parent=parent)  # This will call QWidget.__init__
            self.arg = arg
            self.parent = parent
    
            # Setup the UI from QDesigner
            ui = Ui_widget()
            ui.setupUi(self.parent)
    
        def setLayout(self, layout, title):
            self.parent.setWindowTitle(title)
            self.parent.setLayout(layout)
    
        def doSomething(self):
            # Do something awesome.
            pass
    
    
    class Widget(Mixin, QtGui.QWidget):
        def __init__(self, parent, arg):
            super(Widget, self).__init__(parent=parent, arg=arg)  # Calls Mixin.__init__
    

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