I have a slot in class B which will connect to signal in class A, but class B will be deconstructed inconstantly, so during class B's deconstruction, should I disconnect the slot or will it be disconnected by QT? If I should manually disconnect the connection, how can I do it?
-
If an object is destroyed and is part of some connection, Qt will take care of the disconnection. – eyllanesc May 21 '18 at 01:49
-
Having some code here might help, showing `A` and `B` and their connection. Have you tried using the return value of [`connect`](http://doc.qt.io/archives/qt-5.5/qobject.html#connect) to `disconnect`? – Tas May 21 '18 at 01:57
-
1Possible duplicate of [Are signals in Qt automatically disconnected when one of the class is deleted](https://stackoverflow.com/questions/10570857/are-signals-in-qt-automatically-disconnected-when-one-of-the-class-is-deleted) – thuga May 21 '18 at 08:05
3 Answers
Qt takes care of disconnection. You don't have to worry about it.
But it's recommended to use
obj->deleteLater();
method rather than
delete obj;

- 1,264
- 1
- 11
- 23
You can catch the signal void QObject::destroyed(QObject * obj = 0) to do some clean-up:
This signal is emitted immediately before the object
obj
is destroyed, and can not be blocked.All the objects's children are destroyed immediately after this signal is emitted.
To answer:
should I disconnect the slot or will it be disconnected by QT? If I should manually disconnect the connection, how can I do it?
A signal-slot connection is removed when either of the objects involved are destroyed.

- 11,804
- 3
- 34
- 67
If you are not using thread you can disconnected another class slot like below code here: You will call within constructor of A class not outside of constructor
bttnShutdown = new QPushButton(this);
bttnShutdown->setGeometry(290, 2, 25, 26);
myWorker =new B;
connect(bttnShutdown, SIGNAL(clicked()), myWorker, SLOT(dowork()));

- 135
- 1
- 12
-
What does this have to do with the question? There is nothing about threads mentioned in the question. – thuga May 21 '18 at 08:03