PDA

View Full Version : QThread



TheKedge
23rd August 2006, 20:17
Hello everybody,
I'm using threads ... and I've got a question. I've subclassed QThread and when I call start() the function below is called. Great. Now I thought there's be two ways of doing things: the first way with a forever loop - it works fine; and the second way, with a timer. My question: How do I connect the timer properly?

update() is a func in 'this' i.e. MyClass where I do the work.



void MyClass::run()
{
//can do this ....

forever{
update();
sleep(3);//seconds
}
return;

//or could do this...

QTimer* timer = new QTimer();
timer->setInterval(3000);
connect(timer, SIGNAL(timeout()), this, SLOT(update()), Qt::QueuedConnection);

exec();

delete timer;

return;
}

jacek
23rd August 2006, 20:24
In this case you should use a direct connection, otherwise timeout events will be queued in... the main event loop, since MyClass instance belongs to the GUI thread.

ball
24th August 2006, 03:57
Jacek, do you mean when he want to use the QTimer and exec() method, he need to connect using Qt::DirectConnection in order to let the timer event to be triggered and run in a the same thread as the thread using in run()?

jacek
24th August 2006, 13:02
do you mean when he want to use the QTimer and exec() method, he need to connect using Qt::DirectConnection in order to let the timer event to be triggered and run in a the same thread as the thread using in run()?
Yes, that's what I meant.

TheKedge
24th August 2006, 14:52
Sorry guys,
I don't got it. (using DirectConnection)
The stange thing is that the first variant works fine.
I thought that getting a timer to trigger the work instead of a wait would be a little more elegant. But the timer way doesn't work. I don't get any messages that the connect() doesn't work.

note also (although perhaps beside the point): I have emits in the update() func and they work fine into the gui thread.

any ideas?

thanks
K

jacek
24th August 2006, 14:56
But the timer way doesn't work.
How did you start it?

TheKedge
24th August 2006, 15:15
I call
start()
from the constructor of my class. Or more precisely, I have an init() func called a single shot timer in the constructor. Thus:
QTimer::singleShot(0, this, SLOT(init()));

start() is called from the init() func.

As said, the thread is running -> the loop/sleep method works. But I have the feeling somethings not right with the connect() (although I don't get any error messages)
K

jacek
24th August 2006, 21:05
Does it work with queued connection?

TheKedge
25th August 2006, 11:29
Khryst!
ok ... I'm sorry ... how about using timer->start() ?
It works with both Direct and Queued connections.
thank you, thank you ...
K