title: Qt Threading Ownership
body: <p>I have a Qt Application which connects to X physical devices via bluetooth and does some monitoring. I want to have a kind of ThreadOverseer class which creates a Thread for each device to be monitored as needed and can act upon messages from each device Thread. I have broken my coding style down to this minimal example:</p>
  
  <pre><code>#include &lt;QCoreApplication&gt;
  #include &lt;QList&gt;
  #include &lt;QThread&gt;
  #include &lt;QObject&gt;
  #include &lt;QList&gt;
  #include &lt;QDebug&gt;
  #include &lt;QTimer&gt;
  
  
  
  class DeviceThread : public QThread
  {
      Q_OBJECT
  public:
      DeviceThread(){}
  
      void startThread(){this-&gt;start(QThread::NormalPriority);}
      void stopThread(){
          m_ping_timer-&gt;stop();
          this-&gt;quit();
          if (!this-&gt;wait(3000)) { this-&gt;terminate(); }
      }
  
      void init(int deviceId){m_deviceId = deviceId;
                             this-&gt;moveToThread(this);}
  
      void run(){
          m_ping_timer = new QTimer(this);
          m_ping_timer-&gt;setInterval(5000);
          connect(m_ping_timer, SIGNAL(timeout()), this, SLOT(onPing()));
          m_ping_timer-&gt;start();
  
          this-&gt;exec();
      }
  private slots:
      void onPing(){
          emit newData(m_deviceId);
      }
  
  signals:
      void newData(int deviceId);
  private:
      int m_deviceId;
      QTimer* m_ping_timer;
  };
  
  class ThreadOverseer : public QThread
  {
      Q_OBJECT
  public:
      ThreadOverseer(){}
      void startThread(){
          this-&gt;start(QThread::NormalPriority);
      }
      void stopThread(){
          foreach(int deviceId, m_device_threads.keys())
          {
              DeviceThread* thread = m_device_threads.value(deviceId, nullptr);
              if(thread){
                  thread-&gt;stopThread();
                  delete thread;
                  thread = nullptr;
              }
          }
      }
  
      void run()
      {
          foreach(int deviceId, m_deviceIds)
          {
               DeviceThread* dthread = new DeviceThread();
               dthread-&gt;init(deviceId);
               connect(dthread, SIGNAL(newData(int)), this, SLOT(onNewDeviceData(int)));
               dthread-&gt;startThread();
               m_device_threads.insert(deviceId, dthread);
  
          }
          this-&gt;exec();
      }
  
      void init(QList&lt;int&gt; deviceIds)
      {
          m_deviceIds = deviceIds;
          this-&gt;moveToThread(this);
      }
  
  public slots:
      void onNewDeviceData(int deviceId){
          qDebug() &lt;&lt;  "OVERSEER GOT NEW PING FROM "&lt;&lt; deviceId;
      }
  
  private:
      QList&lt;int&gt; m_deviceIds;
      QMap&lt;int, DeviceThread*&gt; m_device_threads;
  
  };
  int main(int argc, char *argv[])
  {
      QCoreApplication a(argc, argv);
      QList&lt;int&gt; deviceIds;
      deviceIds &lt;&lt; 4 &lt;&lt; 8 &lt;&lt; 42;
  
      ThreadOverseer* overseer = new ThreadOverseer();
      overseer-&gt;init(deviceIds);
      overseer-&gt;startThread();
  
      return a.exec();
  }
  
  </code></pre>
  
  <p>I have read the Qt documentation multiple times. It looks like in cases that where you want to paralize one-shot type tasks you create a QObject which does the stuff needed and move it to a QThread (<a href="https://doc.qt.io/qt-5/qthread.html#details" rel="nofollow noreferrer">https://doc.qt.io/qt-5/qthread.html#details</a>).
  The docs also mention in case that if I want to send data from the ThreadOverseer to slots in my deviceThreads the worker-object approach is not the best, and subclassing is also an option. So I created the above example. </p>
  
  <p>However this this->moveToThread(this) inside the thread seems really dirty to me. Is there a better way to do this, do you have any suggestions?</p>