title: send data and events from pthread to a QT QApplication with QLabel and QImage
body: <p>The question is about interfacing an existing C, pthread-based application, with QT GUI. It seems a simple task to do, but while I have searched (both on Internet and through trial&amp;error), I did not find an acceptable solution.</p>
  
  <p>Now, the application makes heavily use of a pre-existing C library, to which I have no access to modify its code.</p>
  
  <p>Basically, the pre-existing library implements a loop, that calls in a periodic manner some functions (with some data). It is only those functions (function pointers) that I am able to define.</p>
  
  <p>In a complete application, the pre-existing library loop calls those functions (aka "init", "process" and "uninit") in the following manner:</p>
  
  <pre><code> init(f);
   process(f);
   process(f);
   ...
   process(f);
   ...
   process(f);
   uninit(f);
  </code></pre>
  
  <p>The "f" is a structure that allows keeping relevant data from call to call and "init" and "uninint" functions serve to set up and to destroy data allocated by f.</p>
  
  <p>Now, the process() function makes some computationa nd the final result is a matrix, that I would like to display, as an image, inside a QT GUI (as simply as it gets).</p>
  
  <p>However, the QT GUI has its own, blocking, event-loop, aka myApp.exec().</p>
  
  <p>So, to achieve that, I created a new thread (pthread) in my "init" function that will first fill "f" fields with relevant data and then enter the myApp.exec() loop. The myApp is a simple QApplication containing a QLabel that display a QPixmap constructed from a QImage that I am able to fille its "bits()" data with the computed data (using some mapping irrelevant here).</p>
  
  <p>After that, the "process" function makes its successive computations, updates the "bits" data of the QImage (the "bits" pointer is known through the f structure) and then should force the GUI to update itself.</p>
  
  <p>Here are some code snippets:</p>
  
  <pre><code> init(f){
   ...
   pthread_create(&amp;(f-&gt;qt_tid),NULL,&amp;qtimgdisplay_main_thread,f);
   ...
  }
  
   process(f){
   ...
   do_computation();
   do_ update(f-&gt;bits);
   f-&gt;myLabel-&gt;repaint();
  ...
  }
  
  static void *qtimgdisplay_main_thread(void *arg){
      FStructure *f = (FStructure *)arg;
  
      char  arg0[] = "programName";
      char  arg1[] = "arg";
      char  arg2[] = "another arg";
      char* argv[] = { &amp;arg0[0], &amp;arg1[0], &amp;arg2[0], NULL };
      int   argc   = (int)(sizeof(argv) / sizeof(argv[0])) - 1;
  
      f-&gt;myApp = new QApplication(argc, &amp;argv[0]);
  
      //we create a image
      QImage image(f-&gt;winwidth,f-&gt;winheight,QImage::Format_RGB32);
  
      f-&gt;bits=image.bits(); //pointer to the underlying data image
  
      f-&gt;myLabel=new QLabel();
      f-&gt;myLabel-&gt;setPixmap(QPixmap::fromImage(image));
      f-&gt;myLabel-&gt;show();
  
      f-&gt;myApp-&gt;exec();
  
      return NULL;
  }
  </code></pre>
  
  <p>Although somewhat working (execpts some random segmentation faults), the problem with that solution is that it (correctly) displays:</p>
  
  <p>QPixmap: It is not safe to use pixmaps outside the GUI thread</p>
  
  <p>which is perfectly true, since QWidgets are not re-entrant.</p>
  
  <p>So, then I tried to post a Repaint() message to myApp, in an asynchronous manner (I still have to imagine how to emit a signal from my process() to the QLabel myLabel), by replacing, inside "process":</p>
  
  <pre><code> f-&gt;myLabel-&gt;repaint();
  </code></pre>
  
  <p>with:</p>
  
  <pre><code> f-&gt;myApp-&gt;postEvent(f-&gt;myLabel,new QPaintEvent(f-&gt;myLabel-&gt;rect()));
  </code></pre>
  
  <p>The disastrous result is that the QT GUI does not updates itself; more, console displays:</p>
  
  <p>QPainter::begin: Widget painting can only begin as a result of a paintEvent</p>
  
  <p>Finally, I tried to replace the same above line with something "smarter" (that I found here: <a href="http://www.qtforum.org/article/18253/qpaintevent-problem-in-qt4.html" rel="nofollow">http://www.qtforum.org/article/18253/qpaintevent-problem-in-qt4.html</a>), namely:</p>
  
  <pre><code>    QPaintEvent *pe = new QPaintEvent(f-&gt;myLabel-&gt;rect());
      f-&gt;myLabel-&gt;setAttribute(Qt::WA_WState_InPaintEvent, true);
      f-&gt;myApp-&gt;sendEvent(s-&gt;myLabel,f-&gt;myLabel-&gt;rect());
      f-&gt;myLabel-&gt;setAttribute(Qt::WA_WState_InPaintEvent, false);
  </code></pre>
  
  <p>The fantastic result is that it does not even compile:</p>
  
  <p>error: no matching function for call to ‘QApplication::sendEvent(QLabel*&amp;, QRect)’</p>
  
  <p>I am lost. Could anybody, please, help?</p>
  
  <p>These are some possible solutions that I contemplated:</p>
  
  <p>1) making qtimgdisplay_main_thread a QThread thread instead of a pthread thread, although I am not sure this will be of any help;</p>
  
  <p>2) trying to emit some signals from my "process" to the f->myApp or to f->myLabel but I have no idea how to do that since "process" is running on a non-QThread (and there is no way to access the parent thread)</p>
  
  <p>3) create and destroying a new QApplication, with all associated QLabel and QImage inside of "process", but that is simply overwhelming and idiot to do under normal circumstances.</p>
  
  <p>I thank you for bearing reading all this. Please try to provide me a hint through code (not another documentation), if possible.</p>
  
  <p>Edit: It appears that if I use:</p>
  
  <pre><code>f-&gt;myLabel-&gt;update();
  </code></pre>
  
  <p>instead of:</p>
  
  <pre><code> f-&gt;myLabel-&gt;repaint();
  </code></pre>
  
  <p>inside my "process".</p>
  
  <p>Can anybody confirm this is the right solution?</p>