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. Is there notification when event loop starts?
Forum Updated to NodeBB v4.3 + New Features

Is there notification when event loop starts?

Scheduled Pinned Locked Moved General and Desktop
9 Posts 5 Posters 4.6k 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.
  • P Offline
    P Offline
    PedroCaliente
    wrote on last edited by
    #1

    I have a Qt application with this kind of main()...

    @int main(int argc, char *argv[])
    {
        QApplication app(argc, argv);
        MainWindow   mainWin;
    
        ... A separate, non-GUI thread is launched here
    
        mainWin.Init();
        mainWin.show();
    
        app.exec();
    }@
    

    This other thread that is created before the mainWin needs to know when it can start communicating with the mainWin. But since the mainWin uses Qt signals, slots, timers, etc, it's not truly ready to rock until the event loop is running (via exec()).

    My question is: is there some signal or event that is emitted when the event loop has started?

    Consider this. In mainWin.Init(), you can create something like a QTimer and even call .start() to kick it off. But it won't actually be run and trigger events until exec() has been called. This is why I need to know when the event loop has truly started.

    1 Reply Last reply
    0
    • A Offline
      A Offline
      andre
      wrote on last edited by
      #2

      No, there is no real notification mechanism. What you could do, is create a custom event that you post. As soon as that event arrives, you can be sure that your eventloop has started.

      1 Reply Last reply
      0
      • P Offline
        P Offline
        p-himik
        wrote on last edited by
        #3

        You can easily create such notification with QMetaObject::invokeMethod() called with Qt::QueuedConnection connection type. Something like this:
        @
        // separate thread is launched here
        QMetaObject::invokeMethod( notifier, SLOT(notifyThread()), Qt::QueuedConnection );
        return app.exec();@

        notifier->notifyThread() will be queued for execution and will be executed when main event loop starts.

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

          A common solution is to use a single shot timer:

          @
          int main(int argc, const char* argv[])
          {
          QApplication a(argc, argv);
          MyThread thread;
          QTimer::singleShot(0, &thread, SLOT(startYourWork()));

          // ....
          
          a.exec();
          

          }
          @

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

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

            [quote author="p-himik" date="1326702789"]You can easily create such notification with QMetaObject::invokeMethod() called with Qt::QueuedConnection connection type. Something like this:
            @
            // separate thread is launched here
            QMetaObject::invokeMethod( notifier, SLOT(notifyThread()), Qt::QueuedConnection );
            return app.exec();@

            notifier->notifyThread() will be queued for execution and will be executed when main event loop starts.[/quote]

            This would not work, as it would be executed when the thread event loop runs, not the main one.

            [quote author="Volker" date="1326715140"]A common solution is to use a single shot timer:
            @
            int main(int argc, const char* argv[])
            {
            QApplication a(argc, argv);
            MyThread thread;
            QTimer::singleShot(0, &thread, SLOT(startYourWork()));

            // ....
            
            a.exec();
            

            }
            @
            [/quote]

            Take care, where the slot is executed. This one will be executed in the main thread if thread is not moved to itself. If it is moved, the same as the upper comment comes into the game, the thread event loop would be taken, the gui event loop might not have started.

            Nokia Certified Qt Specialist.
            Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

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

              [quote author="Gerolf" date="1326716683"]
              Take care, where the slot is executed. This one will be executed in the main thread if thread is not moved to itself.
              [/quote]

              "You're doing it wrong™":/wiki/Threads_Events_QObjects :-)

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

              1 Reply Last reply
              0
              • P Offline
                P Offline
                p-himik
                wrote on last edited by
                #7

                [quote author="Gerolf" date="1326716683"]This would not work, as it would be executed when the thread event loop runs, not the main one.
                [/quote]

                Sorry I didn't write it but notifier is a separate object created in main() which only purpose is to notify separate thread.

                1 Reply Last reply
                0
                • G Offline
                  G Offline
                  giesbert
                  wrote on last edited by
                  #8

                  [quote author="Volker" date="1326716983"]
                  [quote author="Gerolf" date="1326716683"]
                  Take care, where the slot is executed. This one will be executed in the main thread if thread is not moved to itself.
                  [/quote]

                  "You're doing it wrong™":/wiki/Threads_Events_QObjects :-)
                  [/quote]

                  I did not say do it, I just mentioned the possible problems :-)

                  Nokia Certified Qt Specialist.
                  Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

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

                    [quote author="Gerolf" date="1326717793"][quote author="Volker" date="1326716983"]
                    [quote author="Gerolf" date="1326716683"]
                    Take care, where the slot is executed. This one will be executed in the main thread if thread is not moved to itself.
                    [/quote]

                    "You're doing it wrong™":/wiki/Threads_Events_QObjects :-)
                    [/quote]

                    I did not say do it, I just mentioned the possible problems :-)[/quote]

                    Agreed. But one has those with every signal/slot connection once threads are involved.

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

                    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