Results 1 to 5 of 5

Thread: Timer not connecting to private SLOT

  1. #1
    Join Date
    Oct 2008
    Posts
    306
    Thanks
    6
    Thanked 9 Times in 8 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Timer not connecting to private SLOT

    This is my header:
    Qt Code:
    1. #ifndef ALARMH_H
    2. #define ALARMH_H
    3.  
    4. class Alarm : public QObject
    5. {
    6. Q_OBJECT
    7.  
    8. public:
    9. Alarm(QString file)
    10. {
    11. // code here
    12. }
    13. int parseXML(QDomDocument doc, QString file)
    14. {
    15. // code here
    16. }
    17. void checkAlarm()
    18. {
    19. QTimer *timer = new QTimer(this);
    20. connect(timer, SIGNAL(timeout()), this, SLOT(checkTime()));
    21. qDebug() << timer->timerId();
    22. timer->start(100);
    23. }
    24. private:
    25. QString alarmName;
    26. QTime alarmTime;
    27. QDate alarmDate;
    28. QString alarmSound;
    29. QString alarmMessage;
    30. QString alarmApp;
    31. QString alarmAppArg;
    32.  
    33. signals:
    34. void soundAlarm();
    35.  
    36. private slots:
    37. void checkTime()
    38. {
    39. qDebug() << "check ....";
    40. }
    41.  
    42.  
    43. };
    44. #endif // ALARMH_H
    To copy to clipboard, switch view to plain text mode 

    I use it in .cpp:
    Qt Code:
    1. Alarm a(filename);
    2. a.checkAlarm();
    To copy to clipboard, switch view to plain text mode 

    But the timer somehow does not call the SLOT once it's timed out. Probably my mistake, but where?

  2. #2
    Join Date
    Dec 2007
    Posts
    628
    Thanks
    3
    Thanked 89 Times in 87 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Timer not connecting to private SLOT

    Please create your timer variable class level. So now your header will be:
    Qt Code:
    1. #ifndef ALARMH_H
    2. #define ALARMH_H
    3.  
    4. class Alarm : public QObject
    5. {
    6. Q_OBJECT
    7. QTimer *timer;
    8. public:
    9. Alarm(QString file)
    10. {
    11. // code here
    12. }
    13. int parseXML(QDomDocument doc, QString file)
    14. {
    15. // code here
    16. }
    17. void checkAlarm()
    18. {
    19. timer = new QTimer(this);
    20. if(!connect(timer, SIGNAL(timeout()), this, SLOT(checkTime()))
    21. {
    22. qDebug()<<"Critical!! Cannot connect";
    23. }
    24.  
    25. qDebug() << timer->timerId();
    26. timer->start(100);
    27. }
    28. private:
    29. QString alarmName;
    30. QTime alarmTime;
    31. QDate alarmDate;
    32. QString alarmSound;
    33. QString alarmMessage;
    34. QString alarmApp;
    35. QString alarmAppArg;
    36.  
    37. signals:
    38. void soundAlarm();
    39.  
    40. private slots:
    41. void checkTime()
    42. {
    43. qDebug() << "check ....";
    44. }
    45.  
    46.  
    47. };
    48. #endif // ALARMH_H
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    Oct 2008
    Posts
    306
    Thanks
    6
    Thanked 9 Times in 8 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Timer not connecting to private SLOT

    Still no sign of the SLOT being called.

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Timer not connecting to private SLOT

    You are creating your object on the stack. If it's inside some scope, like a function or method other than main, it will go out of scope and be destroyed.
    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.


  5. #5
    Join Date
    Oct 2008
    Posts
    306
    Thanks
    6
    Thanked 9 Times in 8 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Timer not connecting to private SLOT

    Ok, I finnaly understand the whole heap/stack stuff now. Thanks wysota.
    It's working now.
    Off topic:
    What do you think about the Class? Will having , let's say, 20 new Alarm classes affect the programs usage of computers rescources to much?

Similar Threads

  1. Replies: 2
    Last Post: 24th October 2009, 23:45
  2. Connecting signal to custom slot?
    By dbrmik in forum Qt Tools
    Replies: 2
    Last Post: 30th April 2009, 09:28
  3. Replies: 2
    Last Post: 24th March 2008, 16:59
  4. Replies: 1
    Last Post: 1st November 2007, 14:09
  5. Replies: 2
    Last Post: 4th May 2006, 19:17

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.