title: PySide: redirect stdout to a dialog
body: <p>I have a pyside application which runs a function in a QThread. This function often uses <code>print</code>. How can I redirect the stdout to a dialog (containing a <code>qtextedit</code> or similar) which will display when the function is run.</p>
  
  <p>Here is a minimal example:</p>
  
  <pre><code>class Main(QtGui.QWindow):
    def __init__(self):
        super(Main, self).__init__()
        self.run_button = QtGui.QPushButton("Run")
  
        mainLayout = QtGui.QVBoxLayout()
        mainLayout.addWidget(self.run_button)
        self.setLayout(mainLayout)
  
        self.run_button.clicked.connect(self.run_event)
  
  
    def run_event(self):
        # Create the dialog to display output
        self.viewer = OutputDialog()
  
        # Create the worker and put it in a thread
        self.worker = Worker()
        self.thread = QtCore.QThread()
        self.worker.moveToThread(self.thread)
        self.thread.started.connect(self.worker.run)
        self.worker.finished.connect(self.thread.quit)
  
        self.thread.start()
  
  
  class Worker(QtCore.QObject):
    finished = QtCore.Signal()
  
    def run(self):
      for i in range(10):
        print "Im doing stuff" 
        time.sleep(1)
      self.finished.emit()
  
  
  class OutputDialog(QtGui.QDialog):
    def __init__(self, parent=None):
  
      super(OutputDialog, self).__init__(parent)
      self.text_edit = QtGui.QTextEdit()
      mainLayout = QtGui.QVBoxLayout()
      mainLayout.addWidget(self.text_edit)
  
      self.setLayout(vbox)
  </code></pre>
  
  <p>I can modify the worker to redirect stdout to itself, and then emit the text as a signal:</p>
  
  <pre><code>class Worker(QtCore.QObject):
    finished = QtCore.Signal()
    message = QtCore.Signal(str)
  
   def __init__(self):
     super(Worker, self).__init__()
  
     sys.stdout = self
  
   def run(self):
      for i in range(10):
        print "Im doing stuff" 
        time.sleep(1)
      self.finished.emit()
  
  
   def write(self, text):
       self.message.emit(text)
  </code></pre>
  
  <p>But when I connect this signal to the <code>OutputDialog</code> instance, the text is only displayed once the worker has finished.</p>
  
  <p>I have also tried implementing the method here:
  <a href="https://stackoverflow.com/questions/21071448/redirecting-stdout-and-stderr-to-a-pyqt4-qtextedit-from-a-secondary-thread?lq=1">Redirecting stdout and stderr to a PyQt4 QTextEdit from a secondary thread</a></p>
  
  <p>But it just causes my app to freeze. </p>
  
  <p>Any ideas?</p>