PDA

View Full Version : Using QTimer in QThreads



tzhu
18th March 2009, 10:51
Hi, I just started learning Qt several days ago, and I got stuck on this for a while now. I'm trying to create a new thread in which a QTimer would run, and connect the timer's timeout() signal to a function running in that thread. Here is one of the things that I tried:

I try to run a timer here connected to a slot in MyObject (onTimeOut)


class MyThread : public QThread
{
public:
MyThread();
~MyThread();

QTimer *timer;
void run();
};

void MyThread::run()
{
MyObject object;
timer = new QTimer;
connect(timer, SIGNAL(timeout()), &object, SLOT(onTimeOut()));
timer->start(500);
exec();
}

To my understanding this should call onTimeOut every half second, but that's not happening. Is there something that I'm missing completely?

^NyAw^
18th March 2009, 10:54
Hi,

Does "MyObject" class inherit from QObject and has declared the macro "Q_OBJECT" ?
What is returning the "connect" statment? just add a boolean value as return value and also check the console output.

tzhu
18th March 2009, 11:12
Doh I did forget Q_OBJECT in MyObject! Now I'm embarassed to have wasted 2 hours on that... :o