title: Start a new thread from the constructor of a QMainWindow in Qt
body: <p>I have the <code>MainWindow</code> class. In the constructor of this class I want to start a new thread that will do some work. But I get this error: </p>
  
  <blockquote>
    <p>Assert failure in QWidget: "Widgets must be created in the GUI thread." </p>
  </blockquote>
  
  <p>In this new thread I am not creating any widgets. This is what I have tried so far. Could someone help me on solving this problem? In don't have experience with signals and slots and I will really appreciate some advises.</p>
  
  <p><strong>newThread.h</strong></p>
  
  <pre><code>#ifndef NEWTHREAD_H
  #define NEWTHREAD_H
  #include &lt;QThread&gt;
  #include "mainwindow.h"
  
  class NewThread : public QThread
  {
      Q_OBJECT
  public:
      explicit NewThread(QObject *parent = 0);
  signals:    
  public slots:
  protected:
      void run();
  };
  
  #endif // NEWTHREAD_H
  </code></pre>
  
  <p><strong>newThread.cpp</strong></p>
  
  <pre><code>#include "newthread.h"
  NewThread::NewThread(QObject *parent) :
      QThread(parent) { }
  
  void NewThread::run(){
      MainWindow m;
      m.updateInBackground();
  }
  </code></pre>
  
  <p><strong>MainWindow.cpp</strong></p>
  
  <pre><code>MainWindow::MainWindow(QStringList applications, QWidget *parent) :
      QMainWindow(parent),
      ui(new Ui::MainWindow)
      {
      ReadFromRegistry read;
      this-&gt;setFixedSize(435,280);
      ui-&gt;setupUi(this);
      appsNames = applications;
      this-&gt;apps = read.getApplicationsFromRegistry(appsNames);
      ui-&gt;updateInBackgroundCkb-&gt;setChecked(false);
      //read from settings.xml the time interval
      QString time = RWXml::readSettingsFile();
      if(time.compare("-1") != 0){
         NewThread th;
         while(true){
              th.start();
              th.sleep(time.toLong(0,10));
         }    
      }
  }
  </code></pre>
  
  <p><strong>EDIT:</strong></p>
  
  <p><strong>main.cpp</strong></p>
  
  <pre><code>#include "mainwindow.h"
  #include &lt;QApplication&gt;
  
  int main(int argc, char *argv[])
  {
      QApplication a(argc, argv);    
  
      QStringList apps;
      QString app = "AppTest1";
      apps.append(app);
      app = "AppTest2";
      apps.append(app);
      app = "AppTest3";
      apps.append(app);    
  
      MainWindow w(apps);
      w.create();
      w.show();
  
      return a.exec();
  }
  </code></pre>
  
  <p>I instantiate the <code>MainWindow</code> in main. But i need to access the method from <code>MainWindow</code> in the run method of the <code>NewThread</code>. That's why it is instantiated in the <code>NewThread</code>. </p>
  
  <p><strong>EDIT:</strong></p>
  
  <pre><code>void MainWindow::updateInBackground(){
  ClientSocket client;
  for(Application ap : getApps()){
  
      QString currentVersion = ap.getAppVersion();
      QString appCode = ap.getAppCode();
      QString appSerial = ap.getAppSerialNo();
      client.connect();
  
      QString message = "2//" + currentVersion + "//"  + appCode + "//"+ appSerial;
  
      //send message to the server
      client.sendMessage(message);
      //receiver message from the server
      QString received = client.receiveMessage();
      //check if the current version is the last one
      if(received.compare("0") != 0){
          //if is not the last one, set the new version            
          ap.setAppVersion(received);
          //set the update date           
          ap.setCurrentDate();
          //write in windows registry
          WriteInRegistry::writeRegistry(ap);
          //update the xml file containg the updates of this application           
          updateXMLFile(ap);
      }
  }
  //read from registry
  ReadFromRegistry read;
  //populate the grid from the MainWindow with the new data
  populateTable(read.getApplicationsFromRegistry(getAppsNames()));
  client.closeConnection();
  }
  </code></pre>