PySide Multiple Inheritance: Inheriting a QWidget and a Mixin

半世苍凉 提交于 2019-12-11 12:24: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 set the layout of the widget.

Example:

Mixin:

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

Widget:

class Widget(Mixin, QtGui.QWidget):
    def __init__(self, parent, arg):
        super(Widget, self).__init__(parent=parent, arg=arg)

This won't work, but doing this through composition works

Widget (Composition):

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

I would like to try to have the widget inherit everything instead of having part of it done through composition. Thanks!


回答1:


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__


来源:https://stackoverflow.com/questions/25373054/pyside-multiple-inheritance-inheriting-a-qwidget-and-a-mixin

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