PDA

View Full Version : Connected QTimer slot not being called



Polnareff
27th July 2010, 00:05
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:


QTimer *pTimer;

MyObj()
{
pTimer = new QTimer(this);
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 :)

Lykurg
27th July 2010, 00:16
Could you post some more lines or your real code. And have you declared mySlot as a slot in the header file?

Polnareff
27th July 2010, 16:03
MySingleton.h

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

class MyObj : public QObject
{
Q_OBJECT

public:
MyObj() {}
~MyObj() {}

bool Initialize()
{
pTimer = new QTimer(this);
connect(pTimer, SIGNAL(timeout()), this, SLOT(MySlot()));
pTimer->start(1000);
}

private slots:
void MySlot()
{
//Stuff
}

private:
QTimer *pTimer;
};

main.cpp

#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.

saa7_go
27th July 2010, 16:15
Maybe, you should create QApplication instance before initialize your singleton class.



int main(int argc, char *argv[])
{
QApplication app(argc, argv);
fuiComponentManager::s_pInstance()->Initialize();
//Load gui and stuff here
}

Polnareff
27th July 2010, 16:55
That was it! I was creating QApplication afterwards. Thanks a lot!