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. Move a class that inherits from QObject with all it objects members to QThread
QtWS25 Last Chance

Move a class that inherits from QObject with all it objects members to QThread

Scheduled Pinned Locked Moved Unsolved General and Desktop
8 Posts 4 Posters 1.0k 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.
  • L Offline
    L Offline
    Lior
    wrote on last edited by
    #1

    hi
    Im trying to understend how to work with qthread and signl-slots of qobject.

    1. how to move class with objects members (timers, qserialport etc) to qthread ? i need to move every object in the class sepertly ?
    2. how to send signal to thread ?

    I want to read serial data in differnt thread , and to be able to start and stop it.
    the read is in some time interval for simplisity.

    for this i want thread with timer , and every time out to read the buffer.

    tnx
    lior

    Pl45m4P jsulmJ 2 Replies Last reply
    0
    • L Lior

      hi
      Im trying to understend how to work with qthread and signl-slots of qobject.

      1. how to move class with objects members (timers, qserialport etc) to qthread ? i need to move every object in the class sepertly ?
      2. how to send signal to thread ?

      I want to read serial data in differnt thread , and to be able to start and stop it.
      the read is in some time interval for simplisity.

      for this i want thread with timer , and every time out to read the buffer.

      tnx
      lior

      Pl45m4P Offline
      Pl45m4P Offline
      Pl45m4
      wrote on last edited by Pl45m4
      #2

      @Lior said in Move a class that inherits from QObject with all it objects members to QThread:

      how to move class with objects members (timers, qserialport etc) to qthread ? i need to move every object in the class sepertly ?

      class->moveToThread(thread).
      Note: If you move a QObject, allocate its members in a function (like init()) after it got moved.
      [Edit: or make every member a direct child of your moved class]
      If you create new data on the heap in c'tor for example, it wont be moved too.

      how to send signal to thread ?

      Like you would send a signal to any other QObject


      If debugging is the process of removing software bugs, then programming must be the process of putting them in.

      ~E. W. Dijkstra

      1 Reply Last reply
      0
      • L Lior

        hi
        Im trying to understend how to work with qthread and signl-slots of qobject.

        1. how to move class with objects members (timers, qserialport etc) to qthread ? i need to move every object in the class sepertly ?
        2. how to send signal to thread ?

        I want to read serial data in differnt thread , and to be able to start and stop it.
        the read is in some time interval for simplisity.

        for this i want thread with timer , and every time out to read the buffer.

        tnx
        lior

        jsulmJ Offline
        jsulmJ Offline
        jsulm
        Lifetime Qt Champion
        wrote on last edited by
        #3

        @Lior said in Move a class that inherits from QObject with all it objects members to QThread:

        how to move class with objects members (timers, qserialport etc) to qthread ?

        You don't move a class to a thread but class instances.
        There are several ways:

        1. The easiest one: create the class instance in the thread (in run() method for example).
        2. Use QThread::moveToThread - in this case all it complex members have to be created after moving to the thread.
        3. Use worker object approach - preferred solution. See https://wiki.qt.io/QThreads_general_usage (it also shows how to use signals/slots across threads)

        https://forum.qt.io/topic/113070/qt-code-of-conduct

        L 2 Replies Last reply
        3
        • jsulmJ jsulm

          @Lior said in Move a class that inherits from QObject with all it objects members to QThread:

          how to move class with objects members (timers, qserialport etc) to qthread ?

          You don't move a class to a thread but class instances.
          There are several ways:

          1. The easiest one: create the class instance in the thread (in run() method for example).
          2. Use QThread::moveToThread - in this case all it complex members have to be created after moving to the thread.
          3. Use worker object approach - preferred solution. See https://wiki.qt.io/QThreads_general_usage (it also shows how to use signals/slots across threads)
          L Offline
          L Offline
          Lior
          wrote on last edited by
          #4
          This post is deleted!
          1 Reply Last reply
          0
          • jsulmJ jsulm

            @Lior said in Move a class that inherits from QObject with all it objects members to QThread:

            how to move class with objects members (timers, qserialport etc) to qthread ?

            You don't move a class to a thread but class instances.
            There are several ways:

            1. The easiest one: create the class instance in the thread (in run() method for example).
            2. Use QThread::moveToThread - in this case all it complex members have to be created after moving to the thread.
            3. Use worker object approach - preferred solution. See https://wiki.qt.io/QThreads_general_usage (it also shows how to use signals/slots across threads)
            L Offline
            L Offline
            Lior
            wrote on last edited by
            #5

            @jsulm
            i know that i move a instance, i didnt explain very well the problem.

            i can add a sample of code , my main problem is , that i added slot of start to my worker class to activate the timer , i get "object::starttimer: timers cannot be started from another thread warning"

            im sending the event from the main window to the worker class.

            the worker is act as object when it get the the event and i not fully understend the relation between qthread and signal-slot mechanisem.

            and when object is created its owner is the thread it created it.

            jsulmJ Pl45m4P 2 Replies Last reply
            0
            • J.HilkJ Offline
              J.HilkJ Offline
              J.Hilk
              Moderators
              wrote on last edited by
              #6

              to add what the others said, you don't have to make a init function or create everything after the move.

              Just make sure that every QObject based class member has your class as parent. the parent will, when moved to an other thread, take all it's children with him/her


              Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


              Q: What's that?
              A: It's blue light.
              Q: What does it do?
              A: It turns blue.

              1 Reply Last reply
              3
              • L Lior

                @jsulm
                i know that i move a instance, i didnt explain very well the problem.

                i can add a sample of code , my main problem is , that i added slot of start to my worker class to activate the timer , i get "object::starttimer: timers cannot be started from another thread warning"

                im sending the event from the main window to the worker class.

                the worker is act as object when it get the the event and i not fully understend the relation between qthread and signal-slot mechanisem.

                and when object is created its owner is the thread it created it.

                jsulmJ Offline
                jsulmJ Offline
                jsulm
                Lifetime Qt Champion
                wrote on last edited by
                #7

                @Lior said in Move a class that inherits from QObject with all it objects members to QThread:

                im sending the event from the main window to the worker class

                How exactly? And make sure the timer in your worker was created in the thread where workes is living (see what @J-Hilk wrote).
                Signals and slots work just fine between threads, just don't force DirectConnection (QueuedConnection is used between threads). So, simply connect the signals and slots.

                https://forum.qt.io/topic/113070/qt-code-of-conduct

                1 Reply Last reply
                3
                • L Lior

                  @jsulm
                  i know that i move a instance, i didnt explain very well the problem.

                  i can add a sample of code , my main problem is , that i added slot of start to my worker class to activate the timer , i get "object::starttimer: timers cannot be started from another thread warning"

                  im sending the event from the main window to the worker class.

                  the worker is act as object when it get the the event and i not fully understend the relation between qthread and signal-slot mechanisem.

                  and when object is created its owner is the thread it created it.

                  Pl45m4P Offline
                  Pl45m4P Offline
                  Pl45m4
                  wrote on last edited by
                  #8

                  @Lior said in Move a class that inherits from QObject with all it objects members to QThread:

                  the worker is act as object when it get the the event and i not fully understend the relation between qthread and signal-slot mechanisem.

                  It's explained in the article linked by @jsulm here as well as in the documentation pages here

                  • https://doc.qt.io/qt-6/threads-qobject.html#signals-and-slots-across-threads

                  and when object is created its owner is the thread it created it.

                  Therefore you move it and make members a child of the moved object or create them later after your object was moved


                  If debugging is the process of removing software bugs, then programming must be the process of putting them in.

                  ~E. W. Dijkstra

                  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