PDA

View Full Version : How to close parent window from child window?



montylee
13th October 2008, 17:39
I have the following problem:

I have a Qt Dialog from which i open another child Qt Dialog. Now if user clicks on some button on the child Dialog and some error occurs, i want to close the child window as well as the parent window. I am able to close the child window using
this->close ()
Since at the time user clicks on the button, the control is in the child window so i can close the child window, but how can i close the parent window?

spirit
13th October 2008, 17:42
if you need that after closing a parent widget, a child widget is closed too, then you need add parent into ctor of a child widget.

daggilli
14th October 2008, 05:00
Can't you connect a signal on destruction of the child window to the close() slot in the parent? That is, something like:


connect(this,SIGNAL(destroyed()),this->parent(),SLOT(close());

assuming, of course that the child window is a genuine Qt child object (its constructor has been passed the parent window.)

aamer4yu
14th October 2008, 06:23
May be you can call qApp->quit() when some error occurs.
QCoreApplication::quit

ankit17.ag
14th October 2008, 06:38
I think that if you emit a user defined signal from the child and catch it in the parent, the you can close the parent. However, due to the parent - child relationship, the child shall also be closed when the parent closes.

montylee
14th October 2008, 12:40
Can't you connect a signal on destruction of the child window to the close() slot in the parent? That is, something like:


connect(this,SIGNAL(destroyed()),this->parent(),SLOT(close());

assuming, of course that the child window is a genuine Qt child object (its constructor has been passed the parent window.)
This doesn't work. this->parent () returns NULL in my case.


May be you can call qApp->quit() when some error occurs.
QCoreApplication::quit
I don't want to close the entire application so i can't use qApp->quit().


I think that if you emit a user defined signal from the child and catch it in the parent, the you can close the parent. However, due to the parent - child relationship, the child shall also be closed when the parent closes.
Yes, this is one of the ways of doing the job. This is specially useful if one wants to close the parent window in response to some event in the child window.

I was able to close the parent window after the child window is closed. I referred this thread:

http://www.qtcentre.org/forum/f-qt-programming-2/t-how-to-regain-parents-window-functionsafter-child-window-has-closed-14009.html

So basically, once the child dialog closes, i call this->close () in the parent function and parent window is closed too.

Thanks to everyone for replying to this thread :)