TheFuzz
TheFuzz

Reputation: 2623

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

Upvotes: 4

Views: 1447

Answers (1)

Chris
Chris

Reputation: 17535

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.

Upvotes: 10

Related Questions