Qt application launched in background, not stealing focus

Alec Jacobson

April 22, 2014

weblog/

We're building a Qt executable (not an app bundle) with cmake. When running it feels like an application, dock icon, menu bar and all, but when it's first launched from the command line with something like:

./my_app_exec

The application opens behind the terminal. The focus stays with the terminal. This was frustrating because then each time I launch the application I need to CMD+SHIFT+TAB to get to the application.

Here's a dirty hack workaround. In my main function just after main_window.show() or whatever I add a bit of apple-specific applescript to do the alt tabbing for me:

mainWin->show();
#ifdef __APPLE__
    system("osascript -e 'tell application \"System Events\" "
      "to keystroke tab using {command down, shift down}'");
#endif