Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [solved] Please help me understand QThread

[solved] Please help me understand QThread

Scheduled Pinned Locked Moved General and Desktop
5 Posts 4 Posters 2.6k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    Alegen
    wrote on last edited by
    #1

    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.

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

    1 Reply Last reply
    0
    • A Offline
      A Offline
      andre
      wrote on last edited by
      #2

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

      1 Reply Last reply
      0
      • Z Offline
        Z Offline
        ZapB
        wrote on last edited by
        #3

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

        Nokia Certified Qt Specialist
        Interested in hearing about Qt related work

        1 Reply Last reply
        0
        • G Offline
          G Offline
          giesbert
          wrote on last edited by
          #4

          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

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

          1 Reply Last reply
          0
          • A Offline
            A Offline
            Alegen
            wrote on last edited by
            #5

            WOW... I feel so much better now.

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

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

            1 Reply Last reply
            0

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved