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. What could be the possible cause of Qtimer failure?
Forum Updated to NodeBB v4.3 + New Features

What could be the possible cause of Qtimer failure?

Scheduled Pinned Locked Moved Unsolved General and Desktop
6 Posts 5 Posters 583 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.
  • J Offline
    J Offline
    John Van
    wrote on last edited by
    #1

    I encountered an issue with QTimer where it failed to work properly in one part of my program, even though it functioned correctly elsewhere.

    	QEventLoop loop2;
    	QTimer::singleShot(500, [&]() {loop2.quit(); });
    	loop2.exec();
    

    I was using an event loop to pause the program for a period, but when the timer expired, it didn’t quit as expected, causing the program to hang at that point. I eventually resolved the issue by replacing it with QThread::msleep(500);.

    C J.HilkJ JonBJ 3 Replies Last reply
    0
    • J John Van

      I encountered an issue with QTimer where it failed to work properly in one part of my program, even though it functioned correctly elsewhere.

      	QEventLoop loop2;
      	QTimer::singleShot(500, [&]() {loop2.quit(); });
      	loop2.exec();
      

      I was using an event loop to pause the program for a period, but when the timer expired, it didn’t quit as expected, causing the program to hang at that point. I eventually resolved the issue by replacing it with QThread::msleep(500);.

      C Offline
      C Offline
      ChrisW67
      wrote on last edited by
      #2

      where it failed to work properly in one part of my program, even though it functioned correctly elsewhere.

      What is the difference between these places? Would I be correct in guessing that the working instances are in the main thread, and those that do not behave as expected are in other threads?

      Just out of interest, is there a reason you did not use the more direct:

      QTimer::singleShot(500, &loop2, &QEventLoop::quit);
      

      BTW, this kind of blocking behaviour is usually an anti-pattern in Qt programs.

      J 1 Reply Last reply
      5
      • J John Van

        I encountered an issue with QTimer where it failed to work properly in one part of my program, even though it functioned correctly elsewhere.

        	QEventLoop loop2;
        	QTimer::singleShot(500, [&]() {loop2.quit(); });
        	loop2.exec();
        

        I was using an event loop to pause the program for a period, but when the timer expired, it didn’t quit as expected, causing the program to hang at that point. I eventually resolved the issue by replacing it with QThread::msleep(500);.

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

        @John-Van said in What could be the possible cause of Qtimer failure?:

        QThread::msleep

        this is prime example what will cause timers to "fail"


        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
        3
        • J John Van

          I encountered an issue with QTimer where it failed to work properly in one part of my program, even though it functioned correctly elsewhere.

          	QEventLoop loop2;
          	QTimer::singleShot(500, [&]() {loop2.quit(); });
          	loop2.exec();
          

          I was using an event loop to pause the program for a period, but when the timer expired, it didn’t quit as expected, causing the program to hang at that point. I eventually resolved the issue by replacing it with QThread::msleep(500);.

          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by
          #4

          @John-Van
          As both the others have said.

          If you want to know how your code is behaving why don't you put a qDebug() in the slot? the you would know whether the timer did not expire/call the slot or whether the loop did not quit.

          1 Reply Last reply
          1
          • C ChrisW67

            where it failed to work properly in one part of my program, even though it functioned correctly elsewhere.

            What is the difference between these places? Would I be correct in guessing that the working instances are in the main thread, and those that do not behave as expected are in other threads?

            Just out of interest, is there a reason you did not use the more direct:

            QTimer::singleShot(500, &loop2, &QEventLoop::quit);
            

            BTW, this kind of blocking behaviour is usually an anti-pattern in Qt programs.

            J Offline
            J Offline
            John Van
            wrote on last edited by
            #5

            @ChrisW67
            “this kind of blocking behaviour is usually an anti-pattern in Qt programs.”
            would you provide a detailed explanation?

            jsulmJ 1 Reply Last reply
            0
            • J John Van

              @ChrisW67
              “this kind of blocking behaviour is usually an anti-pattern in Qt programs.”
              would you provide a detailed explanation?

              jsulmJ Offline
              jsulmJ Offline
              jsulm
              Lifetime Qt Champion
              wrote on last edited by
              #6

              @John-Van said in What could be the possible cause of Qtimer failure?:

              would you provide a detailed explanation?

              Never block using long lasting loops or local event loops. This is usually not needed in event driven applications. Long lasting calculations should be done in other threads.

              https://forum.qt.io/topic/113070/qt-code-of-conduct

              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