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. How will MainWindow get notified when in event loop.
Forum Updated to NodeBB v4.3 + New Features

How will MainWindow get notified when in event loop.

Scheduled Pinned Locked Moved General and Desktop
13 Posts 4 Posters 8.2k Views 1 Watching
  • 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.
  • F Offline
    F Offline
    Franzk
    wrote on last edited by
    #3

    Generally speaking, if you receive a showEvent(), you have a running event loop.

    "Horse sense is the thing a horse has which keeps it from betting on people." -- W.C. Fields

    http://www.catb.org/~esr/faqs/smart-questions.html

    1 Reply Last reply
    0
    • S Offline
      S Offline
      SeedOfLife
      wrote on last edited by
      #4

      Right now my main() looks the typical >>
      @int main(int argc, char *argv[])
      {
      QApplication a(argc, argv);
      MainWindow w;
      w.show();

      return a.exec();
      

      }@

      In the MainWindow constructor I want to initialise everything on the UI. After the a.exec() is called I am certain the app is initialised and in the event loop and want the login to spawn there. Basically my login must be handled once the app is in the event loop.

      I tried using showEvent() but that gets called before the a.exec().
      Tried with changeEvent() and catching ApplicationChange event but even they get fired before a.exec().

      So how do u guys suggest I let my little app know when its in the event loop. I can think of a one shot timer but that is not elegant and wont work at all times.

      1 Reply Last reply
      0
      • G Offline
        G Offline
        goetz
        wrote on last edited by
        #5

        In our application we do it this way:

        @
        int main(int argc, char *argv[])
        {
            QApplication a(argc, argv);
            MainWindow w;
            w.show();
         
        w.slotLogin();

        return a.exec();
        }
        @

        In slotLogin I create a model dialog which runs its own event loop on exec().

        Another, more Qt-ish, approach would be with a single shot QTimer:

        @
        int main(int argc, char *argv[])
        {
            QApplication a(argc, argv);
            MainWindow w;
            w.show();
         
        QTimer::singleShot(0, &w, SLOT(slotLogin()));

        return a.exec();
        }
        @

        The timer times out immediately, but the event is processed only when the event loop has started.

        http://www.catb.org/~esr/faqs/smart-questions.html

        1 Reply Last reply
        0
        • F Offline
          F Offline
          Franzk
          wrote on last edited by
          #6

          Key to all this is that your login dialog won't be shown before you have a running event loop in the first place.

          "Horse sense is the thing a horse has which keeps it from betting on people." -- W.C. Fields

          http://www.catb.org/~esr/faqs/smart-questions.html

          1 Reply Last reply
          0
          • G Offline
            G Offline
            goetz
            wrote on last edited by
            #7

            [quote author="Franzk" date="1299070378"]Key to all this is that your login dialog won't be shown before you have a running event loop in the first place.[/quote]

            Nope. The dialog is shown as soon as you call slot show() and dialog->exec() runs it's own event loop and works as expected. I can remove the app.exec() completely and the login works :-) You're right in sofar that the main window w will be fully functional only after app.exec().

            http://www.catb.org/~esr/faqs/smart-questions.html

            1 Reply Last reply
            0
            • F Offline
              F Offline
              Franzk
              wrote on last edited by
              #8

              Haven't tested, but by looking at the setVisible() code I find it odd if it is drawn as soon as show() is called. setVisible is packed with posting events (showToParent and the likes).

              But hey, I might be wrong :-P

              "Horse sense is the thing a horse has which keeps it from betting on people." -- W.C. Fields

              http://www.catb.org/~esr/faqs/smart-questions.html

              1 Reply Last reply
              0
              • F Offline
                F Offline
                Franzk
                wrote on last edited by
                #9

                Tested it: I'm not wrong:
                @int main(int argc, char *argv[])
                {
                QApplication a(argc, argv);
                Dialog w;
                w.show();
                //return a.exec();
                while (true);
                return 0;
                }
                @
                Does nothing but waiting. Obviously w.exec() would work as it starts an event loop.

                "Horse sense is the thing a horse has which keeps it from betting on people." -- W.C. Fields

                http://www.catb.org/~esr/faqs/smart-questions.html

                1 Reply Last reply
                0
                • G Offline
                  G Offline
                  goetz
                  wrote on last edited by
                  #10

                  [quote author="Franzk" date="1299079610"]Tested it: I'm not wrong:
                  @int main(int argc, char *argv[])
                  {
                  QApplication a(argc, argv);
                  Dialog w;
                  w.show();
                  //return a.exec();
                  while (true);
                  return 0;
                  }
                  @
                  Does nothing but waiting. Obviously w.exec() would work as it starts an event loop.[/quote]

                  I never wrote that the above code works. The dialog is shown (in the sense of "made visible") - nothing more. What works is:

                  @
                  int main(int argc, char *argv[])
                  {
                  QApplication a(argc, argv);
                  Dialog w;
                  w.exec();
                  return 0;
                  }
                  @

                  http://www.catb.org/~esr/faqs/smart-questions.html

                  1 Reply Last reply
                  0
                  • F Offline
                    F Offline
                    Franzk
                    wrote on last edited by
                    #11

                    Different definition of showing then.

                    "Horse sense is the thing a horse has which keeps it from betting on people." -- W.C. Fields

                    http://www.catb.org/~esr/faqs/smart-questions.html

                    1 Reply Last reply
                    0
                    • G Offline
                      G Offline
                      goetz
                      wrote on last edited by
                      #12

                      [quote author="Franzk" date="1299081352"]Different definition of showing then.[/quote]

                      Indeed - but nevermind. Calling show() without an exec() afterwards is stupid and only serves for corner case discussions :-) And I'm becoming more and more off topic...

                      Ahm, to get back to the question: I'd prefer the QTimer approach nowadays. This ensures that the even loop is already running - this might be important, depending on the workload in the dialog and other signals/events etc.

                      http://www.catb.org/~esr/faqs/smart-questions.html

                      1 Reply Last reply
                      0
                      • S Offline
                        S Offline
                        SeedOfLife
                        wrote on last edited by
                        #13

                        Hi guys, apologies for not having replied any sooner. Soution to this was to create a single shot timer. It ensured that I had a valid event loop by the time the signal was handled. As for the reason to wait for the event loop, well there were many. But the most prominent was for elegance. I could have put the code for the login dialog inside the MainWindow c'tor but that will look awkward. And besides once the login is handled the initial download should commence which is gonna need the event loop.

                        But what I found out in the process was that the one wudnt have to wait for the exec() call to see if
                        the events are being processed. exec() is merely an endless loop which waits for events and processes them. It's something which the code enters into when it dont have anything to do (like creating windows, setting properties, opening files etc.) and wait until the user quits. I hope this helps out anyone in the future.

                        edit: I wanted to say, single shot timer is more elegant than waiting on show event. Show Events get called even before the exec() is called for the app. But the timer is only fired after exec() is called for.

                        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