So after some time I got it to work.

I spent quite a bit of time trying to get the iterator in addAlarm to cooperate but I never quite got the hang of it so I did it in a way that seemed more familiar to me. Please let me know if my way is less suitable or if I misunderstood the purpose of the iterator.

I also spent some time scratching my head before I realized why the program crashed if the alarm list goes empty. But it seems that that's what happens if one tries to read from list.first in an empty list so I built in protection for that too. Other than that it's pretty much the same as what wysota posted above.

Should anyone else be building an alarm clock or timer of some sort, here is my version (I'm not guaranteeing anything but it seems pretty stable):
Qt Code:
  1. #ifndef REPORTTIMER_H
  2. #define REPORTTIMER_H
  3.  
  4. #include <QtCore>
  5.  
  6. class ReportTimer : public QObject {
  7. Q_OBJECT
  8.  
  9. public:
  10. ReportTimer(QObject *parent = 0) : QObject(parent), m_timer(0) {}
  11. public slots:
  12. void start() {
  13. if(m_timer!=0) return;
  14. m_timer = startTimer(4000); // 4s resolution
  15. }
  16.  
  17. void stop() {
  18. if(m_timer==0) return;
  19. killTimer(m_timer);
  20. m_timer = 0;
  21. }
  22.  
  23. void addAlarm(const QDateTime &dt) {
  24. qDebug() << "in";
  25. if (m_alarms.indexOf(dt) != -1) return; //if already in list, return
  26. else {m_alarms.append(dt); qSort(m_alarms);}
  27. }
  28.  
  29. signals:
  30. void alarm(const QDateTime &dt);
  31.  
  32. protected:
  33. void timerEvent(QTimerEvent *ev) { qDebug() << "Fired" << m_alarms;
  34. if(ev->timerId()!=m_timer) { QObject::timerEvent(ev); return; }
  35. QDateTime current = QDateTime::currentDateTime();
  36. while(m_alarms.isEmpty() == false && m_alarms.first() <= current) {
  37. emit alarm(m_alarms.first());
  38. m_alarms.removeFirst();
  39. }
  40. }
  41.  
  42.  
  43. private:
  44. QList<QDateTime> m_alarms;
  45. int m_timer;
  46. };
  47.  
  48. #endif // REPORTTIMER_H
To copy to clipboard, switch view to plain text mode 

Cheers!
/Tottish