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. Microseconds Delay

Microseconds Delay

Scheduled Pinned Locked Moved Unsolved General and Desktop
5 Posts 5 Posters 769 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.
  • D Offline
    D Offline
    dan1973
    wrote on last edited by dan1973
    #1

    Hi all,

    Given below is my code, with delay() function i have used which takes only ms. i want to give microseconds delay in my loop function.

    I tried with QThread::usleep(100) // for 100us delay. It was holding the gui to take control. i am plotting chart.

    Is there any best method other than QThread to use?

            delay(1000);
            int iCnt = 0;
            while(1) {
                delay(1); // i want to use Microseconds delay here
                if(iCnt < (iTmrIntvl * 2000)) {
                    if(!dstkXVals.isEmpty()) {
                        chrt1->m_yXVal = dstkXVals.pop();
                        chrt1->m_series[0]->append(iCnt, chrt1->m_yXVal);
                        qDebug() << QString::number(iCnt) <<  " XVal: " << chrt1->m_yXVal;
                        ++iCnt;
                    }
                } else {
                    if(DAcqThread::bDAQCmptd) {
                        if(iCnt == (iTmrIntvl * 2000)) {
                            break;
                        }
                    }
                }
            }
    
    

    Please help. Thanks in advance

    jsulmJ Ketan__Patel__0011K S 3 Replies Last reply
    0
    • D dan1973

      Hi all,

      Given below is my code, with delay() function i have used which takes only ms. i want to give microseconds delay in my loop function.

      I tried with QThread::usleep(100) // for 100us delay. It was holding the gui to take control. i am plotting chart.

      Is there any best method other than QThread to use?

              delay(1000);
              int iCnt = 0;
              while(1) {
                  delay(1); // i want to use Microseconds delay here
                  if(iCnt < (iTmrIntvl * 2000)) {
                      if(!dstkXVals.isEmpty()) {
                          chrt1->m_yXVal = dstkXVals.pop();
                          chrt1->m_series[0]->append(iCnt, chrt1->m_yXVal);
                          qDebug() << QString::number(iCnt) <<  " XVal: " << chrt1->m_yXVal;
                          ++iCnt;
                      }
                  } else {
                      if(DAcqThread::bDAQCmptd) {
                          if(iCnt == (iTmrIntvl * 2000)) {
                              break;
                          }
                      }
                  }
              }
      
      

      Please help. Thanks in advance

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

      @dan1973 It does not matter what the interval is: such a loop is blocking the Qt event loop! Do not do such things in event driven applications. Move this stuff into a thread.

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

      1 Reply Last reply
      4
      • D dan1973

        Hi all,

        Given below is my code, with delay() function i have used which takes only ms. i want to give microseconds delay in my loop function.

        I tried with QThread::usleep(100) // for 100us delay. It was holding the gui to take control. i am plotting chart.

        Is there any best method other than QThread to use?

                delay(1000);
                int iCnt = 0;
                while(1) {
                    delay(1); // i want to use Microseconds delay here
                    if(iCnt < (iTmrIntvl * 2000)) {
                        if(!dstkXVals.isEmpty()) {
                            chrt1->m_yXVal = dstkXVals.pop();
                            chrt1->m_series[0]->append(iCnt, chrt1->m_yXVal);
                            qDebug() << QString::number(iCnt) <<  " XVal: " << chrt1->m_yXVal;
                            ++iCnt;
                        }
                    } else {
                        if(DAcqThread::bDAQCmptd) {
                            if(iCnt == (iTmrIntvl * 2000)) {
                                break;
                            }
                        }
                    }
                }
        
        

        Please help. Thanks in advance

        Ketan__Patel__0011K Offline
        Ketan__Patel__0011K Offline
        Ketan__Patel__0011
        wrote on last edited by
        #3

        @dan1973 You can also go with Qt Concurrent Functionality.

        1 Reply Last reply
        0
        • D dan1973

          Hi all,

          Given below is my code, with delay() function i have used which takes only ms. i want to give microseconds delay in my loop function.

          I tried with QThread::usleep(100) // for 100us delay. It was holding the gui to take control. i am plotting chart.

          Is there any best method other than QThread to use?

                  delay(1000);
                  int iCnt = 0;
                  while(1) {
                      delay(1); // i want to use Microseconds delay here
                      if(iCnt < (iTmrIntvl * 2000)) {
                          if(!dstkXVals.isEmpty()) {
                              chrt1->m_yXVal = dstkXVals.pop();
                              chrt1->m_series[0]->append(iCnt, chrt1->m_yXVal);
                              qDebug() << QString::number(iCnt) <<  " XVal: " << chrt1->m_yXVal;
                              ++iCnt;
                          }
                      } else {
                          if(DAcqThread::bDAQCmptd) {
                              if(iCnt == (iTmrIntvl * 2000)) {
                                  break;
                              }
                          }
                      }
                  }
          
          

          Please help. Thanks in advance

          S Offline
          S Offline
          SimonSchroeder
          wrote on last edited by
          #4

          @dan1973 said in Microseconds Delay:

          Is there any best method other than QThread to use?

          Use a QTimer. On timeout() you can then run just a single iteration of your loop. iCnt would then become a member variable. In between timeouts the GUI can still run. However, this also means that you will sometimes get larger delays than specified. If the delay should never become too long you need to go with separate thread (also possible with QtConcurrent::run as mentioned before).

          1 Reply Last reply
          0
          • aha_1980A Offline
            aha_1980A Offline
            aha_1980
            Lifetime Qt Champion
            wrote on last edited by
            #5

            Given that screens refresh with 50 or 100 Hertz, microsecond update rates are useless.

            I'd use a QTimer with an resolutio of 10 milliseconds, for example. Then draw a bunch of points at once. That should give the best performance.

            Regards

            Qt has to stay free or it will die.

            1 Reply Last reply
            4

            • Login

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