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

Can't terminate QApplication before MainWindows is shown.



  • Good evening,
    I have an odd problem. In the code below:

    MainWindow::MainWindow(QWidget *parent) :
        QMainWindow(parent),
        ui(new Ui::MainWindow)
    {
        ui->setupUi(this);
        setConnections();
        loadSettings();
        setViews();
    }
    

    method loadSettings() verifies the settings and if it can't setup the environment to work, should display the message and terminate the program ASAP.
    I used qApp->quit(); this, however doesn't work until the MainWindow is visible.
    I'd like to add that I know I can terminate the thread however I'd prefer the program to terminate gracefully after providing the message to the user.
    I'd be very grateful for any advice how to handle this.

    Kind Regards,
    Artur


  • Moderators

    qApp->quit() exits the event loop. Since you're in the MainWindow's constructor there's no event loop yet nad nothing to quit.

    In main() you usually have something like this:

    QApplication a(argc, argv);
    MainWindow w;
    w.show();
    a.exec();
    

    so what you want to do is not enter that event loop at all and let main() just end. Something along the lines of:

    QApplication a(argc, argv);
    MainWindow w;
    if (w.whatever())
    {
       w.show();
       a.exec();
    }
    

    where whatever() checks some flag you set in that constructor of MainWindow.



  • @Chris-Kawa loadSettings() might be time consuming so I think utilizing signal/slot mechanism would be better. Thanks for the hint, will go on with that!


  • Moderators

    Signals/slots don't change much if loadSettings() is blocking. Setting a flag or emiting a signal would basically achieve the same.
    If you can't just show some startup banner you would have to spin up another thread and then enter that event loop in main thread and wait for a signal to either show main window or quit.



  • @Chris-Kawa I've already sorted the problem in the actual code, works like a charm. Many thanks for your help!


Log in to reply