[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.



  • Did you read the "Wiki article":http://developer.qt.nokia.com/wiki/ThreadsEventsQObjects on this?



  • [quote author="Alegen" date="1302271588"]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.
    [/quote]

    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.

    [quote author="Alegen" date="1302271588"]
    Also, about the moveToThread method of QObject. When I call that, the signals emitted by the object will run in another thread?
    [/quote]

    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.

    [quote author="Alegen" date="1302271588"]
    What about calling a method of the object? In which thread will that method be executed?
    [/quote]

    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.

    [quote author="Alegen" date="1302271588"]
    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.
    [/quote]

    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.



  • bq. 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



  • WOW... I feel so much better now.

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


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.