Pyside6 how to disconnect connection?
-
Hi
You could call disconnect on it before deleting it ?
https://doc.qt.io/qtforpython/PySide6/QtCore/QObject.html#PySide6.QtCore.PySide6.QtCore.QObject.disconnect -
Hey
Heh about 5 mins after posting it I figured it out sigh! Yeah u were right. It was just
self.scene().pinReleaseEvent.disconnect(self.pinReleaseEvent)
Not quite sure why they don't auto disconnect. But I'm having a hell of a time to properly delete objects from QGraphicsScene...When I use shiboken6, does it also remove child items? Like child graphics/proxy widgets attached to that widget too or just the main widget? I keep on getting
RuntimeError: Internal C++ object (layerWidgetHelper) already deleted.
-
@eyllanesc said in Pyside6 how to disconnect connection?:
@Dariusz Why do you say The problem is that my signal is still "connected" somehow? could you provide a minimal and reproducible example
I wish I could but that is a problem in quite "large" app. I'm trying to clean it up with memory now and object deletion. As far as I could tell removing item from view & deleting it. If I interacted with view he would still call the item function & crash.
-
@Dariusz I think that the problem is another, or your implementation does not generate the behavior you expect, so it is necessary that you provide an MRE in order to analyze the cause or solution of the problem. I have not asked you for the code of your project, if your project is small then it can be an MRE, but I have asked you to create another script focused on the functionality that generates problems.
-
This might fall into that lambda exception I hinted at. If
self
is a QGraphicsItem, that isn't derived from QObject, and as such doesn't get the automatic disconnection behavior.https://doc.qt.io/qt-6/qobject.html#connect-4 is the C++ equivalent.
-
@jeremy_k said in Pyside6 how to disconnect connection?:
This might fall into that lambda exception I hinted at. If
self
is a QGraphicsItem, that isn't derived from QObject, and as such doesn't get the automatic disconnection behavior.https://doc.qt.io/qt-6/qobject.html#connect-4 is the C++ equivalent.
I think that you are right. QGraphicsItem is not QObject, thus it does not have auto clean up functionality. I believe that's why I have to manually clean up connections.
-
@eyllanesc said in Pyside6 how to disconnect connection?:
Note: Using shiboken2.delete does not remove the python object (the wrapper) but only the handled C++ object.
This is frustrating... I miss C++ aff!
So shibokeh delete the C++ obejct, how do I delete python Object?del self
? -
@Dariusz said in Pyside6 how to disconnect connection?:
In python you can't delete an object,del
only deletes the variable name but not the object(the memory space). I don't understand why deleting an item must imply deleting the connection that is in the scope of the scene. -
@Dariusz said in Pyside6 how to disconnect connection?:
@eyllanesc said in Pyside6 how to disconnect connection?:
Note: Using shiboken2.delete does not remove the python object (the wrapper) but only the handled C++ object.
This is frustrating... I miss C++ aff!
So shibokeh delete the C++ obejct, how do I delete python Object?del self
?That, or remove all references to the item. If the object isn't referenced via a variable that is in scope somewhere else, the python garbage collector should eventually clean up.
-
@eyllanesc said in Pyside6 how to disconnect connection?:
@jeremy_k Exactly, that's why I think the OP has an XY problem
Well the initial signal problem is now solved. Since I delete items and scene remain active, scene does not clean up connections. So I have to manually clean them up.
My second problem is how to properly delete a qgraphicsitem with its children in python. Perhaps I should make another topic for that as this one has solved my main issue.
-
@Dariusz I don't understand how you have solved your current problem, and so far I don't understand what the connection (or disconnection) of the pinReleaseEvent signal of the scene has to do with the elimination of an item since they are 2 different objects. If anyone understands please explain it to me
-
@eyllanesc said in Pyside6 how to disconnect connection?:
@Dariusz The connection you show is not made by the QGraphicsItem but by the scene, so the one that removes the connection in the last instance is the scene.
The issue is that the QGraphicsItem is on the receiving end of the connection, and is effectively captured. The caller doesn't know that the captured item is no longer valid, leading to an invalid condition at call time. In C++, it would be undefined behavior. I don't know what the python terminology is.
Generic C++ example:
int *i = new int; *i = 0; auto f = [i]() { *i++; }; delete i; f();
-
@jeremy_k The question is the following: in bindings such as PySide or PyQt, the ownership of the items is held by the scene, so when you remove the item from the scene then the item has no ownership and is eventually removed by the python GC (python is not C++), and at the time the GC removes the python object it also removes its connections.