[PyQt] Two base classed inherit QWidget

dboddie at trolltech.com dboddie at trolltech.com
Mon Feb 2 12:52:11 GMT 2009


On Mon Feb 2 10:43:13 GMT 2009, simozack wrote:
> 2009/1/31, Filip Gruszczyński <gruszczy at gmail.com>:
> > Could someone explain to me, how is that possible and why it works:
> >
> > class A(QWidget):
> >  def __init__(self):
> >    QWidget.__init__(self)
> >
> > class B(QWidget):
> >  def __init__(self):
> >    QWidget.__init__(self)
> >
> > class C(A, B):
> >  def __init__(self):
> >    A.__init__(self)
> >    B.__init__(self)
> >
> > I mean, it's cool, that it works - I just used and I love it. But
> > having just C(QWidget, QWidget) won't work. Why above example does?
>
> I think it works because A and B are two different and separated
> instances of the same object (QWidget) while QWidget is the same
> class.

I think it's just a happy accident. Try changing one of the base classes to
QFrame or QLabel - things aren't so happy now.

> Try to substitute QWidget with the python standard 'object' class and
> you have the same behaviour and this error:
>
> 'duplicate base class object'

Using other Qt classes as base classes doesn't produce this error, but I
encountered a segfault when I quit the interactive session I was using.
Another feature is that, although you can get away with inheriting from
QWidget and QFrame (for example), you can only access the methods of the
first base class.

This is probably related to the restriction in C++ that you can only inherit
from one QObject-based class when using multiple inheritance. This is partly
due to the limitations of the meta-object system, but problems also arise as
a result of the parent-child memory management mechanism.

David



More information about the PyQt mailing list