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. QTimer recursively called

QTimer recursively called

Scheduled Pinned Locked Moved General and Desktop
8 Posts 2 Posters 3.7k 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.
  • W Offline
    W Offline
    walolinux
    wrote on last edited by
    #1

    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?

    1 Reply Last reply
    0
    • M Offline
      M Offline
      MuldeR
      wrote on last edited by
      #2

      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?)

      My OpenSource software at: http://muldersoft.com/

      Qt v4.8.6 MSVC 2013, static/shared: http://goo.gl/BXqhrS

      Go visit the coop: http://youtu.be/Jay...

      1 Reply Last reply
      0
      • W Offline
        W Offline
        walolinux
        wrote on last edited by
        #3

        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.

        1 Reply Last reply
        0
        • M Offline
          M Offline
          MuldeR
          wrote on last edited by
          #4

          Please post some code...

          My OpenSource software at: http://muldersoft.com/

          Qt v4.8.6 MSVC 2013, static/shared: http://goo.gl/BXqhrS

          Go visit the coop: http://youtu.be/Jay...

          1 Reply Last reply
          0
          • W Offline
            W Offline
            walolinux
            wrote on last edited by
            #5

            @
            //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?

            1 Reply Last reply
            0
            • M Offline
              M Offline
              MuldeR
              wrote on last edited by
              #6

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

              /* .... */
              }@

              My OpenSource software at: http://muldersoft.com/

              Qt v4.8.6 MSVC 2013, static/shared: http://goo.gl/BXqhrS

              Go visit the coop: http://youtu.be/Jay...

              1 Reply Last reply
              0
              • W Offline
                W Offline
                walolinux
                wrote on last edited by
                #7

                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.

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

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

                  My OpenSource software at: http://muldersoft.com/

                  Qt v4.8.6 MSVC 2013, static/shared: http://goo.gl/BXqhrS

                  Go visit the coop: http://youtu.be/Jay...

                  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