3

そのメソッドを使用するためにwidget1を継承したいのですが、次のようになります。

"TypeError: Error when calling the metaclass bases Cannot create a 
consistent method resolution order (MRO) for bases widget1, QWidget"

プログラムを実行するとき。なぜこれが起こるのか説明してもらえますか?

感謝。

from PyQt4.QtGui import *
from PyQt4.QtCore import *
from PyQt4 import QtCore, QtGui
import sys

class widget1(QtGui.QWidget):
    def __init__(self):
        QtGui.QWidget.__init__(self)

class widget2(QtGui.QWidget, widget1):
    def __init__(self):
        QtGui.QWidget.__init__(self)


if __name__ == '__main__':
    app = QtGui.QApplication(sys.argv)
    test = widget1()
    test.show()
    sys.exit(app.exec_()) 
4

1 に答える 1

3

PyQt4の多重継承

複数のQtクラスからサブクラス化する新しいPythonクラスを定義することはできません。

使用できる代替設計の決定は複数あるため、複数のQObjectの継承は不要です。

単一の親クラスからの単純な継承

class widget1(QtGui.QWidget):
    def __init__(self):
        super(widget1, self).__init__()

    def foo(self): pass
    def bar(self): pass

class widget2(widget1):
    def __init__(self):
        super(widget2, self).__init__()

    def foo(self): print "foo"
    def baz(self): pass

構成

class widget2(QtGui.QWidget):
    def __init__(self):
        super(widget2, self).__init__()
        self.widget1 = widget1()

クラスの1つをミックスインクラスにします。これはQObjectではありません。

class widget1(QtGui.QWidget):
    def __init__(self):
        super(widget1, self).__init__()

    def foo(self): print "foo"
    def bar(self): pass

class MixinClass(object):
    def someMethod(self):
        print "FOO"

class widget2(widget1, MixinClass):
    def __init__(self):
        super(widget2, self).__init__()

    def bar(self): self.foo()
    def baz(self): self.someMethod()
于 2012-07-04T03:59:00.697 に答える