Multiple inheritance for QGraphicsItem

Phil Thompson phil at riverbankcomputing.com
Mon Jun 10 15:45:41 BST 2024


On 10/06/2024 04:39, Maurizio Berti wrote:
> As we all know, PyQt doesn't allow multiple inheritance of more Qt 
> classes,
> except when explicitly allowed (eg. QTextObjectInterface).
> 
> When using the Graphics View framework, though, it is sometimes useful 
> to
> have a QGraphicsItem subclass that also provides QObject features: for
> example, a QGraphicsRectItem that emits a signal whenever its geometry
> changes.
> 
> Right now, whenever I need something similar, I normally choose one of 
> the
> following options, depending on my needs and code simplicity:
> 
> - create a dummy QObject subclass and use an instance of it as a member 
> of
> the actual QGraphicsItem one, then eventually emit its signals whenever
> necessary;
> - create a dummy QGraphicsObject subclass as the actual one to be used,
> with the "real" items as its children, then implementing boundingRect()
> (possibly returning childrenBoundingRect()) and a no-op paint(), and
> eventually "copying" the actual child item common functions by proper
> implementation, simple monkey patching or even __getattr__ override;
> 
> While both solutions "work", they are not really acceptable, nor robust 
> or
> reliable:
> 
> - the dummy QObject "proxy" is an unnecessary difficulty that 
> complicates
> access to the actual signal source; using Qt properties is a further
> complication (consider the case of using QPropertyAnimation);
> - the "main QGraphicsObject" approach adds complexity and further
> difficulties in geometry/transform management and access to the 
> actually
> intended item;
> - issues with the lifespan of the QObject and the "real" QGraphicsItem,
> including connected signals (eg. using QTimers or animations);
> 
> Qt actually considers and allows such multiple inheritance (it's the 
> very
> nature of QGraphicsObject).
> 
> So, my question is quite simple: would it be possible to allow proper
> multiple inheritance for QGraphicsItem+QObject?
> Therefore, would that implicitly allow multiple inheritance for all
> QGraphicsItem inherited classes (the standard ones and those created in
> Python, including mixins)?
> 
> Thanks,
> MaurizioB

It's technically possible by creating a C++ class derived from QObject 
and QGraphicsRectItem and wrapping that (and the same for every other 
QGraphicsItem), but I'm not going to. I'm not going to start creating a 
higher-lever API above Qt.

Phil


More information about the PyQt mailing list