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:
Code:
MyObj()
{
connect(pTimer, SIGNAL(timeout()), this, SLOT(MySlot()));
pTimer->start(1000);
}
MySlot()
{
//stuff here
}
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 :)
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?
Re: Connected QTimer slot not being called
MySingleton.h
Code:
class MySingleton
{
protected:
fuiComponentManager() {}
~fuiComponentManager() {}
static MySingleton *s_pInstance()
{
if( s_pObjInstance == 0 )
s_pObjInstance = new MySingleton();
return s_pObjInstance;
}
bool Initialize()
{
pMyObj = new MyObj();
pMyObj->Initialize();
}
private:
static MySingleton *s_pObjInstance;
MyObj *pMyObj;
};
MySingleton *MySingleton::s_pObjInstance;
MyObj.h
Code:
{
Q_OBJECT
public:
MyObj() {}
~MyObj() {}
bool Initialize()
{
connect(pTimer, SIGNAL(timeout()), this, SLOT(MySlot()));
pTimer->start(1000);
}
private slots:
void MySlot()
{
//Stuff
}
private:
};
main.cpp
Code:
#include "MySingleton.h"
int main(int argc, char *argv[])
{
fuiComponentManager::s_pInstance()->Initialize();
//Load gui and stuff here
}
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.
Re: Connected QTimer slot not being called
Maybe, you should create QApplication instance before initialize your singleton class.
Code:
int main(int argc, char *argv[])
{
fuiComponentManager::s_pInstance()->Initialize();
//Load gui and stuff here
}
Re: Connected QTimer slot not being called
That was it! I was creating QApplication afterwards. Thanks a lot!