PySide Multiple Inheritance: Inheriting a QWidget and a Mixin
我正在尝试创建一组Pyside类,这些类继承Qwidget、QmainWindow和Qdialog。另外,我想继承另一个类来覆盖一些函数,并设置小部件的布局。
例子:
Mixin:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | class Mixin(object): def __init__(self, parent, arg): 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 |
小装置:
1 2 3 | class Widget(Mixin, QtGui.QWidget): def __init__(self, parent, arg): super(Widget, self).__init__(parent=parent, arg=arg) |
号
这行不通,但要通过作曲才能做到这一点。
小部件(组合):
1 2 3 4 5 6 7 | class Widget(QtGui.QWidget): def __init__(self, parent, arg): super(Widget, self).__init__(parent=parent) mixin = Mixin(parent=self, arg=arg) self.setLayout = mixin.setLayout self.doSomething = mixin.doSomething |
我希望让这个小部件继承所有东西,而不是让它的一部分通过组合完成。谢谢!
保留
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | 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__ |