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. threads and created objects
Forum Updated to NodeBB v4.3 + New Features

threads and created objects

Scheduled Pinned Locked Moved Solved General and Desktop
26 Posts 4 Posters 2.9k Views 4 Watching
  • 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.
  • SGaistS SGaist

    @mzimmers said in threads and created objects:

    I need to pursue this a bit further. So, if I have an object (my worker object) that I move to another thread, is it true that anything created within that object prior to the move "belongs" to the original thread?

    If it's a QObject that is properly parented, it will move with it. Note that classes like QTcpSocket must be created in the new thread. You can however pass the descriptor like shown in the threaded QTcpSocket/Server example.

    mzimmersM Offline
    mzimmersM Offline
    mzimmers
    wrote on last edited by
    #17

    @SGaist said in threads and created objects:

    If it's a QObject that is properly patented, it will move with it.

    This is my current creation:

    class Worker : public QObject
    {
        Q_OBJECT
    private:
        SerialPort m_serial;
    ...
    SerialPort::SerialPort(QObject *parent) : QObject (parent)
    {
    ...
    

    Note that classes like QTcoSocket must be created in the new thread.

    But not in the c'tor, correct? Because that is called prior to the thread move, so it would pertain to the original thread?

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #18

      I fixed the typo of parented (damn autocorrect...)

      Yes, m_serial will not be moved.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      mzimmersM 1 Reply Last reply
      2
      • SGaistS SGaist

        I fixed the typo of parented (damn autocorrect...)

        Yes, m_serial will not be moved.

        mzimmersM Offline
        mzimmersM Offline
        mzimmers
        wrote on last edited by mzimmers
        #19

        @SGaist said in threads and created objects:

        I fixed the typo of parented (damn autocorrect...

        Well, while you're at it..."QTcoSocket?"

        kshegunovK SGaistS 2 Replies Last reply
        0
        • mzimmersM mzimmers

          @SGaist said in threads and created objects:

          I fixed the typo of parented (damn autocorrect...

          Well, while you're at it..."QTcoSocket?"

          kshegunovK Offline
          kshegunovK Offline
          kshegunov
          Moderators
          wrote on last edited by kshegunov
          #20

          If you want the QObjects to to be moved alongside with their parent to another thread, then you must pass them the parent. No parent, no way Qt to know whether the object's supposed to be moved.

          Read and abide by the Qt Code of Conduct

          mzimmersM 1 Reply Last reply
          0
          • mzimmersM mzimmers

            @SGaist said in threads and created objects:

            I fixed the typo of parented (damn autocorrect...

            Well, while you're at it..."QTcoSocket?"

            SGaistS Offline
            SGaistS Offline
            SGaist
            Lifetime Qt Champion
            wrote on last edited by
            #21

            @mzimmers said in threads and created objects:

            @SGaist said in threads and created objects:

            I fixed the typo of parented (damn autocorrect...

            Well, while you're at it..."QTcoSocket?"

            Argle........ Done

            Interested in AI ? www.idiap.ch
            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

            1 Reply Last reply
            0
            • kshegunovK kshegunov

              If you want the QObjects to to be moved alongside with their parent to another thread, then you must pass them the parent. No parent, no way Qt to know whether the object's supposed to be moved.

              mzimmersM Offline
              mzimmersM Offline
              mzimmers
              wrote on last edited by mzimmers
              #22

              @kshegunov I don't know how to do that in this case. If my SerialPort object is defined:

              class SerialPort : public QObject
              {
                  Q_OBJECT
              public:
                  explicit SerialPort(Worker *parent = nullptr);
              ...
              SerialPort::SerialPort(Worker *parent) : QObject(parent)
              

              Then how do I declare an instance of SerialPort in my worker object? If I do it like this:

                  SerialPort m_serial;
              

              Then it doesn't seem to get moved.

              1 Reply Last reply
              0
              • SGaistS Offline
                SGaistS Offline
                SGaist
                Lifetime Qt Champion
                wrote on last edited by
                #23

                The usual: pointer and allocate it in the constructor with a parent.

                Interested in AI ? www.idiap.ch
                Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                mzimmersM 1 Reply Last reply
                0
                • SGaistS SGaist

                  The usual: pointer and allocate it in the constructor with a parent.

                  mzimmersM Offline
                  mzimmersM Offline
                  mzimmers
                  wrote on last edited by
                  #24

                  @SGaist OK, thanks. I had (incorrectly) inferred from kshegunov's post that it somehow could be done as an ordinary member variable.

                  kshegunovK 1 Reply Last reply
                  0
                  • mzimmersM mzimmers

                    @SGaist OK, thanks. I had (incorrectly) inferred from kshegunov's post that it somehow could be done as an ordinary member variable.

                    kshegunovK Offline
                    kshegunovK Offline
                    kshegunov
                    Moderators
                    wrote on last edited by
                    #25

                    @mzimmers No the inference is correct. This shall work, shan't it?

                    class Worker : public QObject
                    {
                        Q_OBJECT
                    public:
                        Worker();
                    private:
                        SerialPort m_serial;
                    };
                    
                    Worker::Worker()
                        : m_serial(this)
                    {
                    }
                    

                    Read and abide by the Qt Code of Conduct

                    mzimmersM 1 Reply Last reply
                    2
                    • kshegunovK kshegunov

                      @mzimmers No the inference is correct. This shall work, shan't it?

                      class Worker : public QObject
                      {
                          Q_OBJECT
                      public:
                          Worker();
                      private:
                          SerialPort m_serial;
                      };
                      
                      Worker::Worker()
                          : m_serial(this)
                      {
                      }
                      
                      mzimmersM Offline
                      mzimmersM Offline
                      mzimmers
                      wrote on last edited by
                      #26

                      @kshegunov well...yes it does. I was missing the " : m_serial(this)" clause. Thanks!

                      1 Reply Last reply
                      1

                      • Login

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