[PyQt] Fwd: Mixin classes and PyQt4

Phil Thompson phil at riverbankcomputing.com
Sun Feb 16 13:27:44 GMT 2014


On 12-02-2014 9:55 pm, Baz Walter wrote:
> On 12/02/14 09:35, Martin Teichmann wrote:
>> Hi Phil, Hi Baz, Hi List,
>>
>> I just wrote a patch as advertised earlier, which checks that the
>> inheritance is correct and that noone attempts to inherit from more
>> than one PyQt base class. It is based on the patch sent earlier.
>> Maybe this is an improvement enough to put my patches in?
>
> This actually only relates to multiple-inheritance. It should never
> be problem for true mixin classes.
>
> The one genuine issue with mixins in PyQt, is that you cannot inherit
> custom signals.
>
> In PySide, I can do this:
>
>>>> from PySide import QtCore, QtGui
>>>> app = QtGui.QApplication([])
>>>> class A(object):
> ...     sig = QtCore.Signal(int)
> ...
>>>> class B(A, QtGui.QLabel):
> ...     def __init__(self):
> ...         super(B, self).__init__()
> ...         self.sig.connect(self.bar)
> ...     def foo(self): self.sig.emit(42)
> ...     def bar(self, i): print(i)
> ...
>>>> b = B()
>>>> b.foo()
> 42
>
> I understand that Qt itself doesn't allow this, but is there a
> technical reason why this is not possible in PyQt? There is a real,
> practical need for this functionality (code reuse), and its galling
> that PySide allows it but PyQt doesn't.

It's technically possible, just a bit of a hack. Consider...

class A(object):
     sigA = pyqtSignal()

class B(A):
     pass

class C(object):
     sigC = pyqtSignal()

class MyLabel(C, QLabel):
     pass

class Z(B, MyLabel):
     pass

It needs to work out that the QMetaObject for Z must include sigA but 
not sigC.

Phil


More information about the PyQt mailing list