Abhishek Agarwal Abhishek Agarwal - 21 days ago 8
C++ Question

How to know child Window is destroyed form parent Window

I have a parent-child window in my Qt application. Parent class is a

QDialog
named A and child class is
QMainWindow
named B. Now I want that whenever B is closed through the 'X' button a signal is to be emitted which can be caught by a slot in class A through which I want certain functionality to be implemented. Is there a predefined signal in Qt I can use?

I want something like this:

B *b=new B;
//some code
connect(b,SIGNAL(destroyed()),this,&A::doSomething);


B also has a
QWidget
which I can use to detect the destroyed signal. How do I implement this? Do I need to emit a custom signal from
~B()
?

Edit: I don't want to destroy the object
b
as this would require a reallocation when I want to recreate the window
B
from
A
and I want to keep the parameters of
b
.

Answer Source

Your solution would only work if you set a Qt::WA_DeleteOnClose attribute to your B widget:

b->setAttribute(Qt::WA_DeleteOnClose);

Another option would be to reimplement close event and emit a custom signal there.