[Development] A new approach for Qt main()

Morten Sorvig Morten.Sorvig at qt.io
Mon Dec 12 22:12:31 CET 2016

> On 9 Dec 2016, at 18:05, Thiago Macieira <thiago.macieira at intel.com> wrote:
> Em sexta-feira, 9 de dezembro de 2016, às 10:44:24 PST, Lars Knoll escreveu:
>> Well, the problem is that the main() entry point is causing huge amounts of
>> issues on at least Android and iOS. We’d help those platforms a lot if we
>> didn’t support this kind of entry point (on those platforms) anymore. But I
>> agree that we can’t break this in Qt 5, but we can prepare for Qt6.
>> I’d propose to define a new entry point that works better on these platforms
>> and offering that as the recommended way for new apps. The best solution is
>> probably a static library that provides callbacks that can be used to
>> initialize things.
> Can we get a description of what those problems are, for those of us who have 
> never developed anything for those OSes, so we're not discussing things in the 
> abstract?

For some background, here’s what typical application startup looks like on macOS, NaCl, and Emscripten:

macOS: Define the application delegate, create instance of it in main()

  // Define application delegate with app lifecycle callbacks
  @interface AppDelegate ()

  @implementation AppDelegate
  - (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
     // Init application here

  - (void)applicationWillTerminate:(NSNotification *)aNotification {
     // Tear down here

  // In main, install application delegate and start the app
  int main(int argc, const char *argv[])
       NSApplication *app = [NSApplication sharedApplication];
       app.delegate = [[AppDelegate alloc] initWithArgc:argc argv:argv];
       return NSApplicationMain(argc, argv);

Native Client: Define pp::CreateModule() and return the application module (which is a
subclass of pp::Module)

  namespace pp {
    Module* CreateModule() {
       return new ApplicationModule();

Emscripten: implement main()

  int main(int argc, const char *argv[) {
     // Init application here
     return 0;

main() should/must return to keep the web page responsive. There is API for
simulating a main that does not return and preserve the stack, see emscripten_set_main_loop()
in the emscripten documentation.


More information about the Development mailing list