[Development] A new approach for Qt main()

Morten Sorvig Morten.Sorvig at qt.io
Wed Dec 14 22:52:43 CET 2016

> On 13 Dec 2016, at 14:51, Edward Welbourne <edward.welbourne at qt.io> wrote:
> Morten Sorvig supplied:
>> For some background, here’s what typical application startup looks
>> like on macOS, NaCl, and Emscripten:
> I've updated [0] to illustrate these.
> [0] https://wiki.qt.io/Application_Start-up_Patterns


>> macOS: Define the application delegate, create instance of it in main()
>> // Define application delegate with app lifecycle callbacks
>> @interface AppDelegate ()
>> @end
>>  @implementation AppDelegate
>>  - (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
>>     // Init application here
>>  }
>>  - (void)applicationWillTerminate:(NSNotification *)aNotification {
>>     // Tear down here
>>  }
>>  @end
> Please say more about what has to happen in these, especially init;
> presumably, we need to add some handlers to something like an event
> queue, to ensure our application gets told what's happening and when to
> respond to it.

You are correct. On the highest level, the “Init” code would be:

    g_application = new QGuiApplicaiton()
    // Init application (create app windows etc)

This gives us a nice ordered startup: The native platform has been initialized,
we initialize Qt, and then call application initialization code.

Digging into QGuiApplicaiton construction, it will among other things select
and load a platform plugin, and then ask that platform plugin to create an
event dispatcher, which hooks into the native event queue.

The Qt event dispatcher implementation on macOS supports integrating with
an already running event loop, which is the case here. Calling app.exec()
is not required (and would not work in the callback).


More information about the Development mailing list