Let functionClass
be a class derived from QObject
. In the class constructor of my QMainWindow
class (which hasn't started any other threads) I have the following code:
QThread workThread;
functionClass *functionClassObj = new functionClass;
cout << functionClassObj->thread()->currentThreadId() << endl; // prints 0x16c
functionClassObj->moveToThread( &workThread );
cout << functionClassObj->thread()->currentThreadId() << endl; // prints 0x16c
Why do the currentThreadId()
functions print the same thing if I make a call to moveToThread()
?
currentThreadId()
is a static member of QThread. This means that
functionClassObj->currentThreadId();
is equivalent to
QThread::currentThreadId();
which means that you're going to get the same return value regardless of any object you use or don't use to call the function.
The function in question returns the id of the currently executing thread, and not the thread affinity of the object that you're trying to call it on.
If you want to get a reference to the thread object that the object has affinity for, use QObject::thread()
instead.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With