Results 1 to 5 of 5

Thread: Connected QTimer slot not being called

  1. #1
    Join Date
    May 2010
    Posts
    41
    Thanks
    12
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Connected QTimer slot not being called

    Hi,

    I have a singleton class, call it MySingleton, which contains a pointer to an object of type MyObj.

    MyObj dervies from QObject, uses the Q_OBJECT macro, and contains the following:

    Qt Code:
    1. QTimer *pTimer;
    2.  
    3. MyObj()
    4. {
    5. pTimer = new QTimer(this);
    6. connect(pTimer, SIGNAL(timeout()), this, SLOT(MySlot()));
    7. pTimer->start(1000);
    8. }
    9.  
    10. MySlot()
    11. {
    12. //stuff here
    13. }
    To copy to clipboard, switch view to plain text mode 

    When debugging, it reaches the constructor, the connection returns true, and the timer should have started successfully. But if I place a breakpoint in MySlot, it's never called.

    I never actually use the singleton instance of MySingleton...but it is initialized on startup, which is evident by the fact that the debugger reaches the MyObj constructor. Is it something with this hierarchy that's causing a problem? I assume it might be, since I have a Timer/slot connection like this in even another class which I have a pointer to in MySingleton. Neither work.

    All help is greatly appreciated

  2. #2
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Connected QTimer slot not being called

    Could you post some more lines or your real code. And have you declared mySlot as a slot in the header file?

  3. #3
    Join Date
    May 2010
    Posts
    41
    Thanks
    12
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Connected QTimer slot not being called

    MySingleton.h
    Qt Code:
    1. class MySingleton
    2. {
    3. protected:
    4. fuiComponentManager() {}
    5. ~fuiComponentManager() {}
    6.  
    7. static MySingleton *s_pInstance()
    8. {
    9. if( s_pObjInstance == 0 )
    10. s_pObjInstance = new MySingleton();
    11. return s_pObjInstance;
    12. }
    13.  
    14. bool Initialize()
    15. {
    16. pMyObj = new MyObj();
    17. pMyObj->Initialize();
    18. }
    19.  
    20. private:
    21. static MySingleton *s_pObjInstance;
    22. MyObj *pMyObj;
    23. };
    24. MySingleton *MySingleton::s_pObjInstance;
    To copy to clipboard, switch view to plain text mode 
    MyObj.h
    Qt Code:
    1. class MyObj : public QObject
    2. {
    3. Q_OBJECT
    4.  
    5. public:
    6. MyObj() {}
    7. ~MyObj() {}
    8.  
    9. bool Initialize()
    10. {
    11. pTimer = new QTimer(this);
    12. connect(pTimer, SIGNAL(timeout()), this, SLOT(MySlot()));
    13. pTimer->start(1000);
    14. }
    15.  
    16. private slots:
    17. void MySlot()
    18. {
    19. //Stuff
    20. }
    21.  
    22. private:
    23. QTimer *pTimer;
    24. };
    To copy to clipboard, switch view to plain text mode 
    main.cpp
    Qt Code:
    1. #include "MySingleton.h"
    2. int main(int argc, char *argv[])
    3. {
    4. fuiComponentManager::s_pInstance()->Initialize();
    5. //Load gui and stuff here
    6. }
    To copy to clipboard, switch view to plain text mode 

    It's not exactly the same (left out some irrelevant lines and changed the class names), but that's pretty much it. As you see, it's not very complicated.

  4. #4
    Join Date
    Jun 2010
    Location
    Salatiga, Indonesia
    Posts
    160
    Thanks
    11
    Thanked 32 Times in 29 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    1

    Default Re: Connected QTimer slot not being called

    Maybe, you should create QApplication instance before initialize your singleton class.

    Qt Code:
    1. int main(int argc, char *argv[])
    2. {
    3. QApplication app(argc, argv);
    4. fuiComponentManager::s_pInstance()->Initialize();
    5. //Load gui and stuff here
    6. }
    To copy to clipboard, switch view to plain text mode 

  5. The following user says thank you to saa7_go for this useful post:

    Polnareff (27th July 2010)

  6. #5
    Join Date
    May 2010
    Posts
    41
    Thanks
    12
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Connected QTimer slot not being called

    That was it! I was creating QApplication afterwards. Thanks a lot!

Similar Threads

  1. How come this slot doesn't get called every second?
    By ShaChris23 in forum Qt Programming
    Replies: 2
    Last Post: 5th November 2009, 23:41
  2. Disconnect slot when another is being connected
    By holst in forum Qt Programming
    Replies: 4
    Last Post: 8th September 2009, 09:49
  3. Slot gets called twice
    By greatgatsby in forum Newbie
    Replies: 7
    Last Post: 20th August 2009, 15:11
  4. SLOT not being called
    By steg90 in forum Qt Programming
    Replies: 4
    Last Post: 6th December 2007, 11:30
  5. Replies: 1
    Last Post: 6th March 2007, 15:27

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.