Application Start-up Patterns

From Qt Wiki
Revision as of 12:44, 13 December 2016 by EdwardWelbourne (talk | contribs) (Add macOS, NaCl, emscripten details from Morten Sørvig's mail)
Jump to navigation Jump to search

There are various ways that different platforms support third-party software (e.g. us and our clients) starting a process. The need to fit into each of these constrains the architecture of Qt. We document them here to help illustrate what's needed from our architecture and what the pain points are in the present architecture.

Traditional C main()

This is the simple entry-point where your application is a function – int main(int count, char *args[]) – that gets called by the environment. Your application is started by main() being called and exits when main() returns. Whatever happens in your main() is all your program does.

Since modern applications are generally interactive, this architecture requires that we support an event loop (which other architectures may provide for us). Archetypical (albeit not quite real) code for it looks like:

int main(int count, char *args[])
{
    QApplication app(count, args); // prepare application global state
    // optional set-up
    return app.exec(); // run event loop
}

macOS / Objective C

  • 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
    
  • 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);
      }
    

NaCl (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()</kdb> in the emscripten documentation.