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



  • 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,


  • Moderators

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



  • 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 ...


  • Moderators

    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.



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


  • Moderators

    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.



  • 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



  • 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.



  • 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!


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.