Yes (of course), the second's thread run() method is :Originally Posted by jacek
Do you want some test code to experiment with, do you need more infos ?
Yes (of course), the second's thread run() method is :Originally Posted by jacek
Do you want some test code to experiment with, do you need more infos ?
That's the run() method, but how do you start that thread?Originally Posted by 0xBulbizarre
I start it from gui thread withOriginally Posted by jacek
Qt Code:
otherThread = new OtherThread(this); otherThread->start();To copy to clipboard, switch view to plain text mode
OtherThread's declaration:
Qt Code:
To copy to clipboard, switch view to plain text mode
Looks OK, did you try with 0 instead of "this"?Originally Posted by 0xBulbizarre
yes, but nothing changes (apparently).Originally Posted by jacek
I think I found something... The slot executed by the singleShot timer shall be executed by the worker thread, but is in fact executed by the GUI thread. So, I deduce that the connection created by the singleShot timer is a DirectConnection instead of a QueuedConnection. That would explain why the slot is executed by the emitting thread.
Here is how I create the worker thread, and how I create the singleShot() timer
Qt Code:
// code from the GUI thread otherThread = new OtherThread(this); otherThread->start(); cout << "In GUI Thread, Thread=" << currentThread() << " ThreadID=" << currentThreadId() << endl;To copy to clipboard, switch view to plain text mode
here is my startThread() function (from the worker thread)
Qt Code:
void OtherThread::startThread(void) { cout << "In startThread() Thread=" << currentThread() << " ThreadID=" << currentThreadId() << endl; // do some job... }To copy to clipboard, switch view to plain text mode
outputs where we can see that the GUI thread executes the slot instead of the worker thread:
In GUI Thread, Thread=0x804f198 ThreadID=3078543040
In startThread() Thread=0x804f198 ThreadID=3078543040
Where is my mistake ? should QTimer::singleShot()'s receiver be something else that my otherThread ?
I saw in the docs that:
If we look at my code above (reproduced below)... we can see that both the sender and the receiver live in the same thread. The sender is the singleShot timer, the receiver is a thread object, also living in the GUI thread.By default, QObject::connect() establishes a direct connection if the sender and the receiver live in the same thread, and a queued connection if they live in different threads.
Qt Code:
otherThread = new OtherThread(this); otherThread->start();To copy to clipboard, switch view to plain text mode
is it correct ? how to solve that problem ?
QThread object lives in the thread that created it. You could split that class into two --- the thread itself and an object that lives within that thread.
0xBulbizarre (20th March 2006)
Ok, I now understand why a QThread should'nt do any business logic directly. I'll try that tonight!
thanks for what I learned in this (forum) Thread!
Try this
otherThread = new OtherThread(this);
otherThread->moveToThread(otherThread);
otherThread->start();
that should work ... Regards Madrich
Bookmarks