title: Widgets must be created in the GUI thread Error !. How to correct the code?
body: <p>I have some experience with C++ but I'm a Qt beginner just for a few days. Look at my simple application code below, that is "main.cpp" which is only code file in the project. The problem is that when I try to run the application it returns me error that widgets must be created in the GUI thread. How to go around this ?. Is it possible to write this application that it works as I want without additional thread ?. If Yes then How ?. Please help. I can't get it working by myself.</p>
  
  <pre><code>#include &lt;QApplication&gt;
  #include &lt;QWidget&gt;
  #include &lt;QBoxLayout&gt;
  #include &lt;QGridLayout&gt;
  #include &lt;QPushButton&gt;
  #include &lt;QSpinBox&gt;
  #include &lt;QSlider&gt;
  #include &lt;QRadioButton&gt;
  #include &lt;QFrame&gt;
  #include &lt;QLCDNumber&gt;
  #include &lt;QProgressBar&gt;
  #include "windows.h"
  
  DWORD WINAPI trd1Entry(LPVOID lpParam) ;
  
  int main(int argc, char **argv)
  {
      QApplication app(argc, argv) ;
      QWidget *window = new QWidget() ;
      window-&gt;resize(500, 400) ;
  
      QHBoxLayout *layout0 = new QHBoxLayout() ;
      QGridLayout *layout1 = new QGridLayout() ;
      QVBoxLayout *layout2 = new QVBoxLayout() ;
      QVBoxLayout *layout3 = new QVBoxLayout() ;
  
      QSlider slider0(Qt::Horizontal), slider1(Qt::Horizontal) ;
      QLCDNumber lcd0, lcd1 ;
      QRadioButton rb0, rb1, rb2 ;
      QPushButton pb0("Reset"), pb1("Quit") ;
      QProgressBar progress0 ;
      QSpinBox spin0, spin1 ;
  
      spin0.setRange(-100, 100) ;
      spin1.setRange(-100, 100) ;
      slider0.setRange(-100, 100) ;
      slider1.setRange(-100, 100) ;
  
      rb0.setText("reset first") ;
      rb1.setText("reset second") ;
      rb2.setText("reset both") ;
  
      layout1-&gt;addWidget(&amp;slider0, 0, 0) ;
      layout1-&gt;addWidget(&amp;spin0, 0, 1) ;
      layout1-&gt;addWidget(&amp;spin1, 1, 0) ;
      layout1-&gt;addWidget(&amp;slider1, 1, 1) ;
      layout1-&gt;addWidget(&amp;lcd0, 2, 0) ;
      layout1-&gt;addWidget(&amp;lcd1, 2, 1) ;
  
      layout2-&gt;addWidget(&amp;rb0) ;
      layout2-&gt;addWidget(&amp;rb1) ;
      layout2-&gt;addWidget(&amp;rb2) ;
      layout2-&gt;addWidget(&amp;pb0) ;
      layout2-&gt;addWidget(&amp;pb1) ;
  
      layout0-&gt;addLayout(layout1) ;
      layout0-&gt;addLayout(layout2) ;
  
      QFrame *frame = new QFrame() ;
      frame-&gt;setLayout(layout0) ;
  
      layout3-&gt;addWidget(frame) ;
      layout3-&gt;addWidget(&amp;progress0) ;
  
      frame-&gt;setFrameShape(QFrame::Box) ;
      frame-&gt;setFrameShadow(QFrame::Raised) ;
      frame-&gt;setLineWidth(1) ;
      frame-&gt;move(10, 10) ;
  
      progress0.setRange(-200, 200) ;
  
      QObject::connect(&amp;spin0, SIGNAL(valueChanged(int)), &amp;slider0, SLOT(setValue(int))) ;
      QObject::connect(&amp;spin1, SIGNAL(valueChanged(int)), &amp;slider1, SLOT(setValue(int))) ;
      QObject::connect(&amp;slider0, SIGNAL(valueChanged(int)), &amp;spin0, SLOT(setValue(int))) ;
      QObject::connect(&amp;slider1, SIGNAL(valueChanged(int)), &amp;spin1, SLOT(setValue(int))) ;
      QObject::connect(&amp;spin0, SIGNAL(valueChanged(int)), &amp;lcd0, SLOT(display(int))) ;
      QObject::connect(&amp;spin1, SIGNAL(valueChanged(int)), &amp;lcd1, SLOT(display(int))) ;
  
      QObject::connect(&amp;pb1, SIGNAL(clicked()), &amp;app, SLOT(quit())) ;
  
      QObject *objs[3] = {&amp;spin0, &amp;spin1, &amp;progress0} ;
  
      HANDLE trd1Handle = CreateThread(NULL, 0, trd1Entry, objs, 0, NULL) ;
      if(trd1Handle == NULL)
      ExitProcess(0) ;
  
      window-&gt;setLayout(layout3) ;
  
      window-&gt;show() ;
      return app.exec() ;
  }
  
  DWORD WINAPI trd1Entry(LPVOID lpParam)
  {
      QObject **objs = static_cast&lt;QObject**&gt;(lpParam) ;
      QSpinBox *spin0 = static_cast&lt;QSpinBox*&gt;(objs[0]) ;
      QSpinBox *spin1 = static_cast&lt;QSpinBox*&gt;(objs[1]) ;
      QProgressBar *progress = static_cast&lt;QProgressBar*&gt;(objs[2]) ;
      HANDLE trd1Handle = GetStdHandle(STD_OUTPUT_HANDLE) ;
      if(trd1Handle == INVALID_HANDLE_VALUE)
      return -1 ;
      int total = 0 ;
      while(1)
      {
          total = spin0-&gt;value() + spin1-&gt;value() ;
          progress-&gt;setValue(total) ;
          Sleep(100) ;
      }
      return 0 ;
  }
  </code></pre>