April 8, 2011

Alegen Alegen
Lab Rat
3 posts

[solved] Please help me understand QThread

 

Hello!
I have some questions to which I cannot find a specific answer regarding this class.
I am not even sure if this is possible, but I will ask: how can I run a method of an object in a sepparate thread? Something like the .NET thread class where in the constructor you specify the method of an object and that method will be run in a sepparate thread.

Also, about the moveToThread method of QObject. When I call that, the signals emitted by the object will run in another thread?What about calling a method of the object? In which thread will that method be executed?
And if I move to thread an object, but it has some signals that are connected to slots in a different thread? I feel lost… I do not understand what this moveToThread method does exactly, or why someone should or should not use it.

 Signature 

<< something smart >>
(signatures always contain something like that)

4 replies

April 8, 2011

Andre Andre
Robot Herder
6422 posts

Did you read the Wiki article [developer.qt.nokia.com] on this?

April 8, 2011

ZapB ZapB
Ant Farmer
1442 posts
Alegen wrote:
Hello! I have some questions to which I cannot find a specific answer regarding this class. I am not even sure if this is possible, but I will ask: how can I run a method of an object in a sepparate thread? Something like the .NET thread class where in the constructor you specify the method of an object and that method will be run in a sepparate thread.

By making sure that the object has affinity with the thread. One way is to simply create your object in the run() function of a QThread sub-class. Is the object you wish to call a method of a QObject derived class and is the method a slot? If so then the moveToThread() approach can also work for you.

Alegen wrote:
Also, about the moveToThread method of QObject. When I call that, the signals emitted by the object will run in another thread?

The signals emitted by the object are executed in the worker thread – a signal is just a function call, nothing more. This does not necessarily mean that any slots connected to those signals execute in that thread. The slots get executed in the thread to which their object has affinity.

Alegen wrote:
What about calling a method of the object? In which thread will that method be executed?

The called function will be executed in the same thread as which the call was made if using a direct function call. If using a queued connection to call a slot (either signal-slot connection or via invokeMethod()) then the slot will get executed in the thread to which the receiving object has affinity.

Alegen wrote:
And if I move to thread an object, but it has some signals that are connected to slots in a different thread? I feel lost… I do not understand what this moveToThread method does exactly, or why someone should or should not use it.

As long as the connections were specified as using Automatic as the connection type (the default) then the slots will get executed in the thread to which the receiving object has affinity.

 Signature 

Nokia Certified Qt Specialist
Interested in hearing about Qt related work

April 8, 2011

Gerolf Gerolf
Hobby Entomologist
3251 posts

Also, about the moveToThread method of QObject. When I call that, the signals emitted by the object will run in another thread?What about calling a method of the object? In which thread will that method be executed?
And if I move to thread an object, but it has some signals that are connected to slots in a different thread? I feel lost… I do not understand what this moveToThread method does exactly, or why someone should or should not use it.

moveToThread has influence on signal/slot connections that have an AutoConnect (Default) state. For this connections, the slots are the invoked by a message loop in the thread of the slot object.

Normal function calls into the object are normal function calls. That means they are done in the callers thread!

The magic is only for signal/slot

 Signature 

Nokia Certified Qt Specialist.
Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

April 8, 2011

Alegen Alegen
Lab Rat
3 posts

WOW… I feel so much better now.

Thank you all! No more errors and I actually know how things work :)

 Signature 

<< something smart >>
(signatures always contain something like that)

 
  ‹‹ [Split] Font color in QTextEdit      Statlic linking of Qt and C++ libraries ››

You must log in to post a reply. Not a member yet? Register here!