QTimer recursively called



  • Hi!

    i am programming a desktop app and i am executing a QTimer every X minutes. Once is trigered i use HTTP get request to retrieve some information and program a schedule to launch videos into QML using dynamic number of Qtimers again and is working fine but only if i only if the first QTimer is launched once.

    Example:
    Local time is 17:00h.
    I launch QTimer 1 now, and is trigerred every 60 minutes.
    I retrieve by HTTP get a JSON with video schedule.
    THis JSON has an hour and a video URL.
    JSON contains:
    17:10 video1.mp4
    17:20 video2.mp4
    17:30 video3.mp4
    17:40 video4.mp4
    17:50 video5.mp4
    18:00 video6.mp4
    18:10 video7.mp4
    18:20 video8.mp4
    18:30 video9.mp4
    18:40 video10.mp4
    18:50 video11.mp4
    ...etc.
    My program launches one timer per hour and video.
    Next time QTimer1 launches, i'll need to launch timer for:
    18:00 video6.mp4
    18:10 video7.mp4
    18:20 video8.mp4
    18:30 video9.mp4
    18:40 video10.mp4
    18:50 video11.mp4
    ...etc.

    but i need to delete old timers because if i dont do it, every video launches 2 times or 3 or 4 etc etc.

    The question is, Do i need to use QThread? How can i delete those timers inside a QThread?



  • If I understand you right, you want the QTimer to "fire" exactly once, rather than periodically. So just set QTimer::setSingleShot() to true and that's it. There should be no need to re-invent QTimer yourself by using a separate thread...

    (BTW: If you allocate a new QTimer instance for each file, you'll still have to delete the "old" instances that are no longer needed, to avoid memory leak! But maybe you can just work with a single QTimer instance and re-use it?)



  • No, what i really need to do is to store the dynamic JSON timers in order to delete them once i've reparsered the JSON every X minutes.



  • Please post some code...



  • @
    //This is the timer wich retrieves me the JSON via REST
    QTimer *timer = new QTimer(this);
    // REST
    connect(timer, SIGNAL(timeout()), this, SLOT(descargaDatos()));
    timer->start(60000);
    @

    @
    //This is the SLOT called every X inutes from above
    void MyClass::onDescargaDatos(QNetworkReply* reply)
    {
    .......
    //Those are the dynamic timmers programatically launched on every finished Timer from above.
    QTimer *timer = new QTimer(this);
    timer->setProperty("hour", obj["hour"].toString());
    timer->setProperty("url", _destino + "prog/" + nombreArchivo);

    // Play the video once finished
    connect(timer, SIGNAL(timeout()), this, SLOT(lanzaVideo()));
    timer->start(segundos*1000);
    timer->setSingleShot(true);

    }
    @

    I want to delete all the timers from the SLOT onDescargaDatos every time the first timer finishes. Do i have to store them in a QThread and then delete the QThread?



  • Nope. In lanzaVideo() I would simply do this:
    @void lanzaVideo(void)
    {
    QTimer timer = dynamic_cast<QTimer>(QObject::sender());
    if(timer)
    {
    timer->deleteLater();
    }

    /* .... */
    }@



  • OK, but there will be some timers that will be scheduled for many hours and onDescargaDatos will be again executed after them have trigerred, i neew to delete the future timers too.



  • You are creating the QTimer's in "single shot" mode, so they are guaranteed to not execute again. Also, since each timer will fire eventually (and exactly once), each timer will be deleted eventually (and exactly once).

    And if you really need to delete all timers immediately, even timers that have not fired yet, simply put pointers to all timers that you create into a QList<QTimer*>. This way it will be trivial to delete them all...


Log in to reply
 

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