Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calling moveToThread() doesn't move QObject to another thread.

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()?

like image 539
TheFuzz Avatar asked Dec 28 '22 10:12

TheFuzz


1 Answers

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.

like image 125
Chris Avatar answered Jan 30 '23 13:01

Chris