View Full Version : closeEvent preventing shutdown

16th October 2007, 20:22
It seems that when I implement the QMainWindow::closeEvent() function, my application blocks shutdown -ie, when I shutdown the computer, it tries to close all the apps but chokes on my Qt app, and so it doesn't shut down. In my closeEvent() function I do create a QMessageBox to ask the user if it's okay to exit. When I remove this QMessageBox out, my app closes on shutdown correctly along with the rest of system. Wondering what might be wrong with my code...

mpMessageBox = new QMessageBox(QMessageBox::Question, "Name",
QString(tr("Are you sure you want to exit?")),
QMessageBox::Yes | QMessageBox::No, this);
connect(mpMessageBox, SIGNAL(destroyed()), this, SLOT(dismissMsgBox()));

Yes, I know it's rather unorthodox to create a message box like this, but it was just so that it would not block the event loop, while still having a modal dialog.

16th October 2007, 20:32
I don't know if this will make much difference but have you tried making your connect statement before executing your show command? Since you use show( ) I would think this wouldn't be the problem, but I like to connect all signals and slots before showing a dialog. If this doesn't work, could you show the code inside your dismissMsgBox( ) SLOT?

I just looked in the Qt docs and the SIGNAL destroyed for a QObject says it passes a QObject* as an argument in the SIGNAL so I am guessing you are not capturing the signal since you are trying to get a signal with no arguments. The code should look like:

connect( mpMessageBox, SIGNAL( destroyed( QObject* ) ), this, SLOT( dismissMsgBox( QObject* ) ) );

Do either of these suggestions work?

16th October 2007, 20:41
Why don't you use exec() instead of show()?
This way you wouldn't need any signals and slots at all.