[solved]QCoreApplication::exit() wont wait for QMessageBox
In my application I need to display a message to the user prior to exit. However this doesn't seem to work.
My application is created from the typical Qt Application template project (so nothing odd there). It connects to a server. When user performs a certain action QMessageBox::critical is called and the connection is terminated from the backend (QTcpSocket gets disconnected). In my socket disconnect handler Ive added a QCoreApplication::exit at the end.
So therefore I've come to the judgement that QCoreApplication::exit does not wait for QMessageBox::critical. Is this a known bug ? Due to this scenario I have to hack my code to make it work as expected (asking socket handler to not exit on disconnections prior to displaying the message box and once the user processes the message box, to exit the application).
sandy.martel last edited by
Why is this a bug? Because calling exit actually exit the application?
Why is calling exit where you actually want it would be considered a "hack"?
Sorry, but you seem to be doing the right thing by calling exit where you want it (after the message box is dismissed).
I think u are correct there. QCoreApplication::exit must exit immediately.
However my interpretation of a bug is that execution never pauses when spawning the QMessageBox::ciritcal().
When the user does an invalid operation the backend sends a message which is interpreted as to display the messagebox. Immediately after sending the message, the backend terminates the connection. So in my frontend code, the message is read from the QTcpSocket and the QMessageBox::critical is called. However since the QTcpSocket disconnects, the disconnect handler is called as soon as the QMessageBox is spawned. So it never gets to be seen by the user as the socket handler class calls QCoreApplication::exit on a disconnect.
However I am able to display a QMessageBox::cirtical and wait for the user to respond and then exit, inside the socket handler class disconnect handler code.
It is pretty weird because execution waits for the QMessageBox::critical and then exits the application inside the Socket Handler class. Whereas anywhere else it would just ignore QMessageBox spawning and continue with the application exit.
To add to sandy.martel, your logic seems to be:
emit someSignalWith(messageContent) << Or is it something else ?
exit application from backend
It might even be from a different thread, is it so ?
In any case, are you sure that exiting the application should be done in your backend ? Doesn't seem that it should be it responsibility, your application could also try to reset the connection and start working again.
I have found out the problem and have marked this problem as resolved. Like mentioned before this is a client sever application and as such the frontend waits for the messages from the backend to respond to user interaction. When a user does something inappropriate a message is sent from the backend and later the connection is disconnected. I expect the event handling to be as follows.
- Receive message to show as a popup Message Box.
- Show the MessageBox. Wait until the user closes the MsgBox.
- Process remaining events.
- Any remaining events in this case would be to handle disconnect from socket. Therefore go to socket disconnect handler fucntion.
- In the socket disconnect handlet funciton, terminate the application.
However in my application the message box never waits for the user to close the MsgBox. Instead the appliction is terminated. I later found out that this application is not driven by Qt's event loop and rather from a custom event loop using Windows Hooks and custom windows msg processing and it seems this is causing the problems.