Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Can't terminate QApplication before MainWindows is shown.
QtWS25 Last Chance

Can't terminate QApplication before MainWindows is shown.

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 2 Posters 271 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • artwawA Offline
    artwawA Offline
    artwaw
    wrote on last edited by
    #1

    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

    For more information please re-read.

    Kind Regards,
    Artur

    1 Reply Last reply
    0
    • Chris KawaC Offline
      Chris KawaC Offline
      Chris Kawa
      Lifetime Qt Champion
      wrote on last edited by Chris Kawa
      #2

      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.

      artwawA 1 Reply Last reply
      3
      • Chris KawaC Chris Kawa

        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.

        artwawA Offline
        artwawA Offline
        artwaw
        wrote on last edited by
        #3

        @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!

        For more information please re-read.

        Kind Regards,
        Artur

        1 Reply Last reply
        0
        • Chris KawaC Offline
          Chris KawaC Offline
          Chris Kawa
          Lifetime Qt Champion
          wrote on last edited by
          #4

          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.

          artwawA 1 Reply Last reply
          2
          • Chris KawaC Chris Kawa

            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.

            artwawA Offline
            artwawA Offline
            artwaw
            wrote on last edited by
            #5

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

            For more information please re-read.

            Kind Regards,
            Artur

            1 Reply Last reply
            0

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved