使用抽象类进行多重继承

时间:2015-04-16 21:56:24

标签: python qt4 multiple-inheritance

我有一个这样的课程

from abc import ABCMeta, abstractmethod

class Observer(object):
    __metaclass__ = ABCMeta

    @abstractmethod
    def update(self, dataMsg):
        pass

Observer.register(tuple)

我需要能够像这样使用Multiply Inherit

class MyTable(QTableWidget, Observer):
    def __init__(self, data):
        QTableWidget.__init__(self, 0, len(data))
        self.setHorizontalHeaderLabels(data)

但如果我这样做,我会收到此错误:

tableColumns = QStringList(['A' , 'B'])
atable = MyTable(tableColumns)

 class MyTable(QTableWidget, Observer):
TypeError: Error when calling the metaclass bases
    metaclass conflict: the metaclass of a derived class must be a (non-strict) subclass of the metaclasses of all its bases

有解决方法吗?

1 个答案:

答案 0 :(得分:0)

我不确定这是否会回来咬我,但我通过聚合而不是继承来解决它。所以相反:

class MyTable(Observer):
    def __init__(self, data):
        self.myTable = QTableWidget(0, len(data))
        self.myTable.setHorizontalHeaderLabels(data)

    def getWidget(self):
        return self.myTable