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 function at a specific time
Forum Updated to NodeBB v4.3 + New Features

[SOLVED]Execute function at a specific time

Scheduled Pinned Locked Moved General and Desktop
10 Posts 5 Posters 13.0k 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.
  • M Offline
    M Offline
    Mr.FreakyJo
    wrote on 16 Feb 2012, 14:04 last edited by
    #1

    How do I call a function at a specific time?
    For example I want to execute function launch() on 18/02/2012 15:35:01 .

    1 Reply Last reply
    1
    • K Offline
      K Offline
      koahnig
      wrote on 16 Feb 2012, 14:14 last edited by
      #2

      If you want to do it through Qt you may want to have a look to "QTimer":http://developer.qt.nokia.com/doc/qt-4.8/qtimer.html

      Vote the answer(s) that helped you to solve your issue(s)

      1 Reply Last reply
      0
      • M Offline
        M Offline
        Mr-Merlin
        wrote on 16 Feb 2012, 14:15 last edited by
        #3

        Hi,

        You could inherit from QTime and poll the current time in the object. When it reaches the time you want, emit a signal.

        Hope that helps ;O)

        1 Reply Last reply
        0
        • M Offline
          M Offline
          Mr.FreakyJo
          wrote on 16 Feb 2012, 14:40 last edited by
          #4

          I was thinking of using QTimer this way:
          Create a QTimer object that emits a signal every second.
          When the signal is emited I remove a seconds from some sort of Countdown timer that when reaches 0 launch the function that I want.I think that this way is consuming too much resources that's why I asked help :),maybe someone knows a better way.My knowledge in QT is not too good but I learn fast.
          Sorry for my bad english.

          1 Reply Last reply
          0
          • M Offline
            M Offline
            Mr-Merlin
            wrote on 16 Feb 2012, 14:52 last edited by
            #5

            There's also QDateTime which is probably better than QTime, as it provides AM/PM too.
            So you could do something like this: -

            @
            class QActOnDateTime : public QDateTime
            {
            Q_OBJECT
            private:

                 int timerID;
                 QDateTime checkTime;
            
                 void Init()
                 {
                     timerId = startTimer(1000);  // start a timer at 1 second intervals
                 }
            
                 void timerEvent(QTimerEvent* evt)
                 {
                     if(evt->timerId() == timerID)
                     {
                         if(currentDateTime() >= checkTime)
                         {
                               // call function due to elapsed time
                               DoSomething();
                         }
                     }
                 }
            

            };
            @

            1 Reply Last reply
            0
            • T Offline
              T Offline
              tobias.hunger
              wrote on 16 Feb 2012, 15:04 last edited by
              #6

              The simplistic "Count down some timer every second" is going to fail when you loose/gain an hour due to daylight saving time.

              Why not get the current QDateTime every minute and only use a QTimer for the last couple of seconds (if you need that kind of resolution)? The fewer wakeups your application triggers the less power your computer is going to consume.

              1 Reply Last reply
              0
              • M Offline
                M Offline
                Mr.FreakyJo
                wrote on 16 Feb 2012, 15:38 last edited by
                #7

                How about this:
                @QDateTime now = QDateTime::currentDateTime();
                QDateTime timeoftheaction;
                QTimer *timer=new QTimer;
                connect(timer, SIGNAL(timeout()),object,SLOT(action);
                timer->start(now.secsTo(timeoftheaction)*1000);@
                Isn't this aproach better?

                1 Reply Last reply
                0
                • T Offline
                  T Offline
                  tobias.hunger
                  wrote on 16 Feb 2012, 15:58 last edited by
                  #8

                  It is... once you managed to abolish daylight saving time and had your user sign a contract stating that he will never set the clock.

                  1 Reply Last reply
                  0
                  • G Offline
                    G Offline
                    giesbert
                    wrote on 16 Feb 2012, 16:26 last edited by
                    #9

                    If you assume, the user changes the clock, you can forget that stuff without "polling via timer".
                    I would convert both times to UTC, to be sure DST is in calculation.

                    Nokia Certified Qt Specialist.
                    Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

                    1 Reply Last reply
                    0
                    • M Offline
                      M Offline
                      Mr.FreakyJo
                      wrote on 16 Feb 2012, 16:29 last edited by
                      #10

                      I'll read the current time from a server and then I'll convert it to UTC.Thank you for your help.

                      1 Reply Last reply
                      0

                      10/10

                      16 Feb 2012, 16:29

                      • Login

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