Unsolved Disconnect QObject signal/slot in Qt5
-
In Qt 4.8.x, As disconnect QObject is called explicitly since disconnectNotify() is not invoked for graceful cleanup, Is it recommended to remove all disconnect of QObject signals/slots associated before deleting QObject since disconnectNotify is invoked automatically in Qt5 or ok to keep disconnect()?
Below snippet is used in Qt 4.8.x,
disconnect();
delete QObject; -
@Ramakanth said in Disconnect QObject signal/slot in Qt5:
ok to keep disconnect()?
It is OK to keep disconnect().
delete QObject;
It is best not to call
delete
on QObjects. UseQObject::deleteLater()
to avoid crashes in your code.When you use
QObject::deleteLater()
, signals and slots are automatically disconnected and the object will be deleted when it is safe.In Qt 4.8.x, As disconnect QObject is called explicitly since disconnectNotify() is not invoked for graceful cleanup
This sounds wrong to me. Where did you hear this from?
-
@JKSH said in Disconnect QObject signal/slot in Qt5:
It is best not to call delete on QObjects. Use QObject::deleteLater() to avoid crashes in your code.
Sorry for the delayed response. Thanks for your response.
There is a bug in Qt 4.8.x https://bugreports.qt.io/browse/QTBUG-4844. Due to which the statement, A signal-slot connection is removed when either of the Qt objects involved are destroyed” is incorrect in Qt 4.8 but is fixed in Qt 5.0.Ok shall retain the existing disconnect() statements used in Qt 4.8.x and call deleteLater() instead. In Qt 5.x, we donot have to call disconnect QObject as it is disconnected automatically,right?
-
@Ramakanth said in Disconnect QObject signal/slot in Qt5:
Qt 5.x, we donot have to call disconnect QObject as it is disconnected automatically,right?
Yes.
You can write some simple test code double-check.