Calling moveToThread () does not transfer the QObject to another thread.

Let functionClass be the class derived from QObject . In the class constructor of my QMainWindow class (which has not started any other threads yet), 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 functions of currentThreadId() print the same thing if I make a call to moveToThread() ?

+4
source share
1 answer

currentThreadId() is a static member of QThread. It means that

 functionClassObj->currentThreadId(); 

equivalently

 QThread::currentThreadId(); 

which means that you will get the same return value regardless of any object that you use or not use to call the function.

The function in question returns the identifier of the current executable thread, not the proximity of the thread of the object you are trying to call.

If you want to get a reference to the thread object with which the object has an affinity, use QObject::thread() instead.

+10
source

Source: https://habr.com/ru/post/1382421/


All Articles