Results 1 to 12 of 12

Thread: Suggestions on how to make an alarmclock-like function

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #9
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,017 Times in 4,793 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Suggestions on how to make an alarmclock-like function

    I don't get why you replaced what I had with your line #26. It's suboptimal. The code just had two typos After correcting them it compiles and works fine. Here is the corrected code:
    Qt Code:
    1. #include <QtGui>
    2. #include <QObject>
    3.  
    4. class AlarmClock : public QObject {
    5. Q_OBJECT
    6. public:
    7. AlarmClock(QObject *parent = 0) : QObject(parent), m_timer(0) {}
    8. public slots:
    9. void start(int ms = 1000) {
    10. if(m_timer!=0) return;
    11. m_timer = startTimer(ms); // 1s resolution
    12. }
    13. void stop() {
    14. if(m_timer==0) return;
    15. killTimer(m_timer);
    16. m_timer = 0;
    17. }
    18. void addAlarm(const QDateTime &dt) {
    19. QList<QDateTime>::iterator iter = qLowerBound(m_alarms.begin(), m_alarms.end(), dt);
    20. if(iter==m_alarms.end() || *iter != dt) m_alarms.insert(iter, dt);
    21. }
    22. signals:
    23. void alarm(const QDateTime &dt);
    24. protected:
    25. void timerEvent(QTimerEvent *ev) {
    26. if(ev->timerId()!=m_timer) { QObject::timerEvent(ev); return; }
    27. QDateTime current = QDateTime::currentDateTime();
    28. while(!m_alarms.isEmpty() && m_alarms.first()<=current) {
    29. emit alarm(m_alarms.first());
    30. m_alarms.removeFirst();
    31. }
    32. }
    33. private:
    34. QList<QDateTime> m_alarms;
    35. int m_timer;
    36. };
    37.  
    38. class Dummy : public QObject {
    39. Q_OBJECT
    40. public slots:
    41. void onAlarm(const QDateTime &dt) {
    42. qDebug() << dt;
    43. }
    44. };
    45.  
    46. #include "main.moc"
    47.  
    48. int main(int argc, char **argv){
    49. QApplication app(argc, argv);
    50. AlarmClock alarms;
    51. Dummy d;
    52. d.connect(&alarms, SIGNAL(alarm(QDateTime)), &d, SLOT(onAlarm(QDateTime)));
    53. alarms.addAlarm(QDateTime(QDate::currentDate(), QTime::currentTime().addSecs(2)));
    54. alarms.addAlarm(QDateTime(QDate::currentDate(), QTime::currentTime().addSecs(6)));
    55. alarms.addAlarm(QDateTime(QDate::currentDate(), QTime::currentTime().addSecs(14)));
    56. alarms.addAlarm(QDateTime(QDate::currentDate(), QTime::currentTime().addSecs(10)));
    57. alarms.start();
    58. return app.exec();
    59. }
    To copy to clipboard, switch view to plain text mode 
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  2. The following user says thank you to wysota for this useful post:

    Tottish (9th March 2011)

Similar Threads

  1. Replies: 1
    Last Post: 15th December 2010, 13:20
  2. Replies: 6
    Last Post: 24th June 2009, 14:27
  3. Replies: 1
    Last Post: 12th January 2009, 18:05
  4. Design suggestions
    By vermarajeev in forum Qt Programming
    Replies: 1
    Last Post: 15th December 2006, 09:22
  5. Log browser suggestions?
    By NRGizeR in forum Qt Programming
    Replies: 3
    Last Post: 14th July 2006, 09:20

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.