[PyQt] Multiple Inheritance with PyQt5 on python 2
Baz Walter
bazwal at ftml.net
Fri Dec 9 19:49:55 GMT 2016
On 09/12/16 12:22, Sebastian Eckweiler wrote:
> Hi there,
>
> I've just read through
> http://pyqt.sourceforge.net/Docs/PyQt5/multiinheritance.html.
> I'm using a self-built PyQt5 for Python 2 on Windows - and it seems I
> can't get multiple inheritance to work:
>
> class SomeClass(object):
>
> def __init__(self, attr):
> self.attr = attr
>
> class AnotherDialog(QtWidgets.QDialog, SomeClass):
>
> def __init__(self, parent=None):
> #super(AnotherDialog, self).__init__(parent) # tested this was
> well
> QtWidgets.QDialog.__init__(self, parent)
> SomeClass.__init__(self, 1)
>
>
> app = QtWidgets.QApplication([])
> dlg = AnotherDialog()
>
> This always results in
>
> TypeError: __init__() takes exactly 2 arguments (1 given)
>
> is there a workaround for this?
The signature of SomeClass has one required positional argument. If you
use super, how is it going to get that argument if you don't pass it to
the AnotherDialog constructor?
The docs you linked to explained that you should use keyword arguments.
So one way to resolve the issue would be like this:
class SomeClass(object):
def __init__(self, attr=1, **kwargs):
super(SomeClass, self).__init__(**kwargs)
self.attr = attr
class AnotherDialog(QtWidgets.QDialog, SomeClass):
def __init__(self, parent=None, **kwargs):
super(AnotherDialog, self).__init__(parent, **kwargs)
Or, less flexibly, perhaps like this:
class SomeClass(object):
def __init__(self, attr):
super(SomeClass, self).__init__()
self.attr = attr
class AnotherDialog(QtWidgets.QDialog, SomeClass):
def __init__(self, parent=None):
super(AnotherDialog, self).__init__(parent, attr=1)
Without super, put the mixin first:
class SomeClass(object):
def __init__(self, attr):
self.attr = attr
class AnotherDialog(SomeClass, QtWidgets.QDialog):
def __init__(self, parent=None):
SomeClass.__init__(self, 1)
QtWidgets.QDialog.__init__(self, parent)
More information about the PyQt
mailing list