[PyQt] Antwort: Re: Multiple Inheritance with PyQt5 on python 2

Sebastian Eckweiler sebastian.eckweiler at twt-gmbh.de
Mon Dec 12 09:17:52 GMT 2016


Hi there, 

thanks - I guess I wasn't really aware of the super-workings with multiple 
inheritance (a good occasion to re-read Hettinger's nlog entry on 
this...).
Things worked just fine though with PyQt4, so this caused some confusion.

Cheers
Sebastian



Von:    Baz Walter <bazwal at ftml.net>
An:     pyqt at riverbankcomputing.com
Datum:  09.12.2016 20:51
Betreff:        Re: [PyQt] Multiple Inheritance with PyQt5 on python 2
Gesendet von:   "PyQt" <pyqt-bounces at riverbankcomputing.com>



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)

_______________________________________________
PyQt mailing list    PyQt at riverbankcomputing.com
https://www.riverbankcomputing.com/mailman/listinfo/pyqt
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://www.riverbankcomputing.com/pipermail/pyqt/attachments/20161212/566dc065/attachment.html>


More information about the PyQt mailing list