title: PySide: QThread event handling
body: <p>I have been researching the following issue on PySide threading, but I have found very little documentation so here goes a long post on my findings hoping for some concise feedback about how it goes.
  Supposing we are building a multi-threaded GUI application that invokes some linux processes.
  We will be using QThread class to call a QProcess from within with the objective of having the application running multiple processess simultaneously when it is ready (in different threads)</p>
  
  <p>I am using just a QThread Class and starting through the QApplication main loop</p>
  
  <p><strong>Case 1</strong>:
  - We connect the thread termination signal to another function of the same class that calls self.exit (so that the thread class exits)
  - No call to self.exec_() as instructed in the official documentation of PySide <a href="http://tinyurl.com/qh7cooa" rel="nofollow">http://tinyurl.com/qh7cooa</a></p>
  
  <pre><code>#!/usr/bin/python3.2
  from PySide import QtGui
  from PySide import QtCore
  import sys, random
  
  class ProcThread(QtCore.QThread):
  
      def __init__(self, parent=None):
          super(ProcThread, self).__init__(parent)
  
      def run(self):
          try:
              self.qproc = QtCore.QProcess()
              self.finished.connect(self.threadFinished)
              filename = "/home/user1/Desktop/fileno"+str(random.randint(1, 10000))+".txt"
              self.qproc.start("touch", [filename])       
          except Exception as error:
              print(str(error))
              raise(error)
  
      #self.exec_()
  
      def threadFinished(self):
          print("Thread has finished")
          self.qproc.close()
  
  app = QtGui.QApplication(sys.argv) 
  procthread = ProcThread()
  procthread.start()
  app.exec_()
  </code></pre>
  
  <p><em>RESULT</em>: The thread termination is caught (message "Thread has finished" gets printed), the process does run (file is created) but the application DOES NOT EXIT</p>
  
  <p><strong>Case 2</strong>: Same as before, but now also calling <code>self._exec()</code> (uncommenting the relevant line in previous code snippet)
  <em>RESULT</em>: The thread termination is NOT caught, the process does run (file is created) and once again the application DOES NOT EXIT (remains as a background job) after Ctrl+Z / have to kill it explicitly</p>
  
  <p><strong>Case 3</strong>: Now connecting the signal emmitted when the <code>QProcess</code> terminates through <code>self.qproc.finished.connect(self.threadFinished)</code>.
  No call to self.exec_()</p>
  
  only citing code between try-except - #self.exec_() remains commented
  
  <pre><code>self.qproc = QtCore.QProcess()
  self.qproc.finished.connect(self.threadFinished)
  filename = "/home/pantelis/Desktop/fileno" + str(random.randint(1, 10000)) + ".txt"
  self.qproc.start("touch", [filename])
  </code></pre>
  
  <p><em>RESULT</em>: As in Case 2 - The thread termination is NOT caught, the process does run (file is created) and once again the application DOES NOT EXIT</p>
  
  <p><strong>Case 4</strong>: As in Case 1, but now adding the following line in <code>threadFinished</code>
  <code>self.exit()</code>
  <em>RESULT</em>: As in Case 2 - The thread termination is NOT caught, the process does run (file is created) and once again the application DOES NOT EXIT</p>
  
  <p><strong>Case 5</strong>: As in Case 4, just uncommenting the <code>self.exec_()</code>
  RESULTS: Thread termination IS CAUGHT (message "Thread has finished" gets printed), file IS created and once again the application DOES NOT EXIT</p>
  
  <p><strong>Cases 6, 7, 8</strong>: Added <code>self.qproc.close() / self.qproc.terminate() / self.qproc.kill()</code> in <code>threadFinished</code> to explicitly terminate the QProcess from the specific function
  <em>RESULT</em>: As in Case 5</p>
  
  <p>One could also try different alternatives, such as linking the <code>self.terminated</code> signal of <code>QThread</code> to <code>threadFinished</code> and so on.
  Some very rough questions although I guess is a complex issue:
  - Why although formal documentation states that call to <code>QThread</code>'s <code>exec_()</code> function is necessary for the thread to start event handling, this seems to be occurring without a call to the specific functions
  - In some cases thread termination is caught / processed by <code>threadFinished</code> and in some it is not
  - most importantly: why doesn't the application terminate? (I have also tried <code>app = QtCore.QCoreApplication(sys.argv)</code>)</p>