Using Signals in QGraphicsItem
-
@adrien-lsh
Since you seem to have provided a nice, standalone, complete example I can copy and paste --- unlike so many other people --- I will give it a play now.... -
@adrien-lsh
Yeah, that's easier said than done! It's so thin, and I can't tell when I have actually grabbed it or not! Anyway I have reproed now, it does aSIGSEGV
from0x00007ffff711fc14 in PySide::SignalManager::emitSignal(QObject*, char const*, _object*) () from /home/jon/.venv/pyside6/lib/python3.12/site-packages/PySide6/libpyside6.abi3.so.6.7 (gdb) bt #0 0x00007ffff711fc14 in PySide::SignalManager::emitSignal(QObject*, char const*, _object*) () at /home/jon/.venv/pyside6/lib/python3.12/site-packages/PySide6/libpyside6.abi3.so.6.7
Looking into that now...
-
@adrien-lsh
I think it is to do with the initialisation, requiring both inherited classes to be initialised. You were only doing so for theQGraphicsLineItem
, not initialising theQObject
. I find the following does work without crashing:class CroppingLine(QObject, QGraphicsLineItem): moved = Signal(object, QPointF) def __init__(self, x1: float, y1: float, x2: float, y2: float): #super(QGraphicsLineItem, self).__init__(x1, y1, x2, y2) QObject.__init__(self) QGraphicsLineItem.__init__(self, x1, y1, x2, y2)
There may be other ways of doing this with
super()
and/or not swapping the inheritance order, I just know this works fine. -
-
Hi,
It's Python3, remove the class from your super calls. This should allow Python to do proper initialisation of the chain of classes (though sometimes it might fail for complex cases).
[Edit: we are in such a complex case]
-
@SGaist
But the OP started with justsuper().__init__(x1, y1, x2, y2)
and, for whatever reason, that segments (later, on signal emit) because the
QObject
part has not been initialized, apparently. That is why I tried splitting it into two separate explicit initializations, which makes it work. So what exactly are you proposing? -
@JonB I misread the code !
We are in the territory where your solution (using
__init__
) is the way to go. Multiple inheritance in Python is quite tricky...QObject expects an optional parent parameter which is not given here but it's not expecting the parameters for the QGraphicsLineItem constructor hence the failure. You can try to use the kwargs trick however it won't work either as it's not part of any of the method signature and thus can't work either.