2

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?

Zain
  • 101
  • 3
  • 6
  • 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
  • 1
    Possible 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 Answers3

1

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;

Same Question Exists

calynr
  • 1,264
  • 1
  • 11
  • 23
0

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.

Joseph D.
  • 11,804
  • 3
  • 34
  • 67
-1

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()));
Subrata Das
  • 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