Refactored qpplication event loop handling for correct exiting behavior.