Correct way to use multiple inheritance with Qt (pysyde6)

159 views Asked by At

I'm trying to use multiple inheritance with my and Qt-derived classes. But faced with the fact that the __init__() method from my class is not called when I use super() to call __init__() for base classes. I suppose that's because Qt classes do not use super() in their constructors.

The example that does not work properly ("BASE INIT" was never printed):

class OptionBase(QWidget):
    def __init__(self, *args, **kwargs):
        print("BASE INIT", self)
        super().__init__(*args, *kwargs)

#----------------------------------------------------------------------
class OptionLineEdit(QLineEdit, OptionBase):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)

I've tried several variants (change ordering of base classes, use metaclass, etc.). But only got either coredump or some strange python errors like: "RuntimeError: '__init__' method of object's base class (OptionLineEdit) not called" for the line where the statement print() is located.

The only way I managed to make it to work is the following:

class OptionBase(QWidget):
    def __init__(self, *args, **kwargs):
        print("BASE INIT", self)

#----------------------------------------------------------------------
class OptionLineEdit(QLineEdit, OptionBase):
    def __init__(self, *args, **kwargs):
        QLineEdit.__init__(self, *args, **kwargs)
        OptionBase.__init__(self, *args, **kwargs)

So, I don't call any further __init__ in my base class (to avoid "double initialization" error). And I have to call all __init__ manually in the child class.

It this correct way in such situation or am I missing something?

P.S. The MRO looks correct for me:

<class 'ui.widgets.option_lineedit.OptionLineEdit'>
<class 'PySide6.QtWidgets.QLineEdit'>
<class 'ui.widgets.option_base.OptionBase'>
<class 'PySide6.QtWidgets.QWidget'>
<class 'PySide6.QtCore.QObject'>
<class 'PySide6.QtGui.QPaintDevice'>
<class 'Shiboken.Object'>
<class 'object'>
0

There are 0 answers