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. [SOLVED] Execute code every X seconds (X not constant)
Forum Updated to NodeBB v4.3 + New Features

[SOLVED] Execute code every X seconds (X not constant)

Scheduled Pinned Locked Moved General and Desktop
9 Posts 4 Posters 2.9k 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.
  • BlackMambaB Offline
    BlackMambaB Offline
    BlackMamba
    wrote on last edited by
    #1

    Hello,

    I have a vector which contains some time values like 0, 1.34, 4.56, etc ... which are not regular.
    I want to be able to execute some code for all these time values.
    The first idea I had was to use a QTimer but how? Because QTimer looks more useful when you need to execute code every fixed time interval with timeout() signal.

    Thanks for your help,

    1 Reply Last reply
    0
    • raven-worxR Offline
      raven-worxR Offline
      raven-worx
      Moderators
      wrote on last edited by
      #2

      is "QTimer::singleShot()":http://qt-project.org/doc/qt-4.8/qtimer.html#singleShot what you are looking for?

      --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
      If you have a question please use the forum so others can benefit from the solution in the future

      1 Reply Last reply
      0
      • BlackMambaB Offline
        BlackMambaB Offline
        BlackMamba
        wrote on last edited by
        #3

        If I use a QTimer, yes it will need to be singleshot but it is not enough to solve my issue.
        Lets say I do timer->start();
        Then how do I parse my vector in real time to know if I need to execute the code?
        @QTimer* timer = new QTimer;
        timer->setSingleShot(true);
        timer->start();
        for(int i=0;i<myvector.size();i++){
        if(timer->ellapsed()=myvector[i]{
        // execute code
        }
        }@

        This looks to be very awkward and I dont know if the "=" in
        @if(timer->ellapsed()=myvector[i] @

        is working ...

        1 Reply Last reply
        0
        • raven-worxR Offline
          raven-worxR Offline
          raven-worx
          Moderators
          wrote on last edited by
          #4

          QTimer::singelShot() is a static method. So you don't need a QTimer instance and thus also not call start().
          The slot you specify will be executed once on timeout.

          --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
          If you have a question please use the forum so others can benefit from the solution in the future

          1 Reply Last reply
          0
          • BlackMambaB Offline
            BlackMambaB Offline
            BlackMamba
            wrote on last edited by
            #5

            Yes but how does this solve my issue?
            I would like to have kind of timeout but at irregular interval ...

            1 Reply Last reply
            0
            • raven-worxR Offline
              raven-worxR Offline
              raven-worx
              Moderators
              wrote on last edited by
              #6

              for example:
              @
              void MyExecuteSlot()
              {
              //DO SOME WORK

              if( values.count() )   //values is of type QList?
                  QTimer::singleShot( values.takeFirst() * 1000, this, SLOT(MyExecuteSlot()) );
              

              }
              @

              I assumed your values are in seconds? (QTimer only accepts milliseconds).

              Please note: I don't know on which OS you are working on, but you will only receive an approximate real time speed on a non realtime OS.

              --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
              If you have a question please use the forum so others can benefit from the solution in the future

              1 Reply Last reply
              0
              • BlackMambaB Offline
                BlackMambaB Offline
                BlackMamba
                wrote on last edited by
                #7

                It looks nice I will try this tonight . I am working on windows. I don't need real time OS, how accurate this timer will be? If the latence is like 10ms, its fine !
                I have a std::vector instead of a QList but you gave me the logic !
                I knew the ravens were very smart ;)

                Thanks

                1 Reply Last reply
                0
                • M Offline
                  M Offline
                  mehrdadsilver
                  wrote on last edited by
                  #8

                  and you can use setTimerType(Qt::PreciseTimer) for better accuracy if you want to use QTimer object (No Static singleshot). after each interval restart timer with next value.

                  Mehrdad Abdolghafari, Be silver

                  1 Reply Last reply
                  0
                  • N Offline
                    N Offline
                    NicuPopescu
                    wrote on last edited by
                    #9

                    good to know you're working on windows ... so you could have a time resolution of nanoseconds ... and I think in this case qint64 QElapsedTimer::nsecsElapsed () const could be used

                    bq. if(timer->ellapsed()=myvector[i]{
                    // execute code
                    }
                    }

                    QTimer even has no ellapsed method

                    on the other hand, I think QTimerEvent or QBasicTimer are lighter/faster than QTimer using signals; anyway all of them provide control just in milliseconds, compared with QElapsedTimer which can work with much higher resolution

                    see also QElapsedTimer::ClockType for windows

                    Cheers!

                    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