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. QTimer timeout signal isn't called when a local event loop is running
QtWS25 Last Chance

QTimer timeout signal isn't called when a local event loop is running

Scheduled Pinned Locked Moved Solved General and Desktop
6 Posts 5 Posters 1.4k 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.
  • robinfauryR Offline
    robinfauryR Offline
    robinfaury
    wrote on last edited by
    #1

    Hi there,

    I need to refresh some data every second. So I use a QTimer to update my data. On my app I have a QMenu as popup. If I exec the QMenu, it run a local QEventLoop. Because of that, my QTimer isn't called.

    Should I need QThread in this case?

    Best regards,
    robin

    J.HilkJ 1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi and welcome to devnet,

      The local event loop created by your menu exec call handles the events in place of the main loop.

      One thing you can do is use the worker object approach and move you data collection code in a different thread.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      3
      • M Offline
        M Offline
        MrShawn
        wrote on last edited by
        #3

        From the QMenu docs:
        "They can be executed either asynchronously with popup() or synchronously with exec()."

        So even though they are getting their own event loop, if you are calling it with exec() then it will stop your event loop that is calling it until it returns something. You should instead try calling it with popup() and connect signals and slots to handle any asynchronous events coming from your popup menu.

        Hope it helps.

        1 Reply Last reply
        3
        • robinfauryR robinfaury

          Hi there,

          I need to refresh some data every second. So I use a QTimer to update my data. On my app I have a QMenu as popup. If I exec the QMenu, it run a local QEventLoop. Because of that, my QTimer isn't called.

          Should I need QThread in this case?

          Best regards,
          robin

          J.HilkJ Offline
          J.HilkJ Offline
          J.Hilk
          Moderators
          wrote on last edited by J.Hilk
          #4

          @robinfaury
          take a look at this example:

          int main(int argc, char *argv[])
          {
          
              QApplication a(argc, argv);
          
              QTimer t;
              QObject::connect(&t, &QTimer::timeout, []()->void{qDebug() <<QTime::currentTime().toString();});
              t.start(10);
          
              QDialog dia;
              dia.exec();
          
              return  a.exec();
          }
          

          you'll notice that the debug output does not stop. So simply calling exec on (in this case) QDialog doesn't block the signal handling.
          There has to be something different going on inside your code


          Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


          Q: What's that?
          A: It's blue light.
          Q: What does it do?
          A: It turns blue.

          1 Reply Last reply
          5
          • robinfauryR Offline
            robinfauryR Offline
            robinfaury
            wrote on last edited by robinfaury
            #5

            @MrShawn Yes I see the popup behaviors but I need a synchronous process and the update.

            @SGaist @J-Hilk I found my mistake
            My QMenu exec is called inside the lambda.

            QApplication a(argc, argv);
            
            QTimer t;
            QObject::connect(&t, &QTimer::timeout, []()->void {
                qDebug() <<QTime::currentTime().toString();
                QDialog dia;
                dia.exec();
            });
            t.start(10);
            
            return  a.exec();
            

            Thanks for everything!

            Update: How can I set the thread as SOLVED?

            aha_1980A 1 Reply Last reply
            2
            • robinfauryR robinfaury

              @MrShawn Yes I see the popup behaviors but I need a synchronous process and the update.

              @SGaist @J-Hilk I found my mistake
              My QMenu exec is called inside the lambda.

              QApplication a(argc, argv);
              
              QTimer t;
              QObject::connect(&t, &QTimer::timeout, []()->void {
                  qDebug() <<QTime::currentTime().toString();
                  QDialog dia;
                  dia.exec();
              });
              t.start(10);
              
              return  a.exec();
              

              Thanks for everything!

              Update: How can I set the thread as SOLVED?

              aha_1980A Offline
              aha_1980A Offline
              aha_1980
              Lifetime Qt Champion
              wrote on last edited by
              #6

              @robinfaury

              Update: How can I set the thread as SOLVED?

              With the button Topic Tools below the last post.

              Regards

              Qt has to stay free or it will die.

              1 Reply Last reply
              1

              • Login

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