PDA

View Full Version : Close All Windows



Tino
2nd August 2009, 17:00
I wrote this to prevent the user from closing the main dialog and leaving other windows open:


void Dialog::closeEvent(QCloseEvent *e) {
if (QMessageBox::question(this, tr("Close ALL windows?"),
tr("Close ALL windows?"), QMessageBox::Yes, QMessageBox::No) == QMessageBox::Yes)
{
qApp->closeAllWindows();
}
}

But when I click yes nothing happens and the program is blocked.
What I do wrong?

franz
2nd August 2009, 17:14
I have no means for a quick test at this moment, but try accepting the event.


void Dialog::closeEvent(QCloseEvent *e) {
if (QMessageBox::question(this, tr("Close ALL windows?"),
tr("Close ALL windows?"), QMessageBox::Yes, QMessageBox::No) == QMessageBox::Yes)
{
qApp->closeAllWindows();
e->accept();
}
}

Tino
2nd August 2009, 20:16
Thank you. but whether I accept or ignore the event, still don't work.
I tried to call qApp->quit() instead of qApp->closeAllWindows(), it seems to work, but the destructor of the other windows opened are not called.

I open this other dialogs like this:

DlgTabla *t = new DlgTabla();
t->show();

A possible solution could be: construct the dialog with parent the main dialog:

DlgTabla *t = new DlgTabla(this);
t->show();
The destructor is called, however all windows I show shares the same element of the main dialog in the task bar.

There another way to close all windows and to call their destructors?
Or as alternative solution, show this other dialogs without sharing the same element in the task bar?

Thanks

fakefish
14th April 2010, 10:50
I think the problem comes from the difference between closeEvent end close methods.
When ever a widget is attempted to be closed, QT calls closeEvent. And if you call another acrion causing close it will recursively call itself

So you SHOULD NOT call qApp->closeAllWindows in close event. just accept the event.
And if you want your app closed whenever your widget is closed, write the aboce closing code to close method.

Happy QTing

aamer4yu
14th April 2010, 14:35
The destructor is called, however all windows I show shares the same element of the main dialog in the task bar.

There another way to close all windows and to call their destructors?
Or as alternative solution, show this other dialogs without sharing the same element in the task bar?

If your widgets are properly parented, I dont see need to even handle closeEvent. From what you posted it seems you want to call the destructor of custom widgets before the application or main window closes.
Am I right in understanding your requirement ?