How to emit a Qt signal daily at a given time?

后端 未结 1 1588
情深已故
情深已故 2020-12-04 01:54

I need to notify some objects to clear their cache at new day begins. So, I could create QTimer or something similar and check every ms that now midnight +-5ms or not, but i

相关标签:
1条回答
  • The simplest solution does indeed utilize a timer. Polling for the passage of time in not only unnecessary, but would be horrible performance-wise. Simply start the actions when the midnight strikes:

    static int msecsTo(const QTime & at) {
      const int msecsPerDay = 24 * 60 * 60 * 1000;
      int msecs = QTime::currentTime().msecsTo(at);
      if (msecs < 0) msecs += msecsPerDay;
      return msecs;
    }
    
    // C++11
    
    void runAt(const std::function<void> & job, const QTime & at, Qt::TimerType type = Qt::VeryCoarseTimer) {
      // Timer ownership prevents timer leak when the thread terminates.
      auto timer = new QTimer(QAbstractEventDispatcher::instance());
      timer->start(msecsTo(at), type);
      QObject::connect(timer, &QTimer::timeout, [=job, &timer]{
        job();
        timer->deleteLater();
      });
    }  
    
    runAt([&]{ object->member(); }, QTime(...));
    
    // C++98
    
    void scheduleSlotAt(QObject * obj, const char * member, const QTime & at, Qt::TimerType type = Qt::VeryCoarseTimer) {
      QTimer::singleShot(msecsTo(at), type, obj, member);
    }
    
    class MyObject : public QObject {
      Q_OBJECT
      void scheduleCleanup() {
        scheduleSlotAt(this, SLOT(atMidnight()), QTime(0, 0));
      }
      Q_SLOT void atMidnight() {
        // do some work here
        ...
        scheduleCleanup();
      }
    public:
      MyObject(QObject * parent = 0) : QObject(parent) {
        ...
        scheduleCleanup();
      }
    };  
    

    there is some other timer which shots every 100ms for instance and it trying to get data from container.

    Since both of these timers presumably run in the same thread, they execute serially and it doesn't matter how much "later" either one is. They won't both run at the same time.

    0 讨论(0)
提交回复
热议问题