Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

How to execute initialization method after QApplication::exec has been called?



  • I'm working with a very simple main method like this:

    int main()
    {
      MyQtApp app;
      return app.exec();
    }
    

    Lets assume my app has to do some very heavy initialization which should happen after application startup (especially without any user interaction to trigger). How can I achieve this?

    If I just do this:

    int main()
    {
      MyQtApp app;
      app.heavy_init();
      return app.exec();
    }
    

    then the even queue won't run and I can't emit signals. If I use a dedicated thread, it's still awkward because once the initialization is finished, I can't join the threads because the main thread will be executing QApplication::exec. Any ideas how to do what I'm looking for?


  • Lifetime Qt Champion

    Hi
    What about a single shot timer ?
    QTimer::singleShot(1000, { app.heavy_init(); } );


Log in to reply