title: Using a QThread in PyQT for serial communication (w. pyserial)
body: <p>I am pretty much a beginner when it comes to GUI programming.
  I am using QT in combination with python bindings (PyQT4).</p>
  
  <p><strong>What I am trying to do:</strong></p>
  
  <ul>
  <li>Setting up a <code>QThread</code> to read from &amp; write to a Serial Port with
  pyserial.</li>
  <li>The main application should be able to emit new serial data via a
  signal to the running <code>QThread</code>. and receive serial data from the
  <code>QThread</code> with a signal.</li>
  </ul>
  
  <p>I started my own test implementation based on this code (<a href="https://stackoverflow.com/a/6789205/2706668" title="Link">Link</a>).
  Prior to this I read the basics about <code>QThread</code>s and tried to understand  how they are intended to be used.
  The following test code is what I have come up with. I m sorry, I tried to keep it minmal, 
  but it is still 75 lines of code:</p>
  
  <pre><code>from PyQt4 import QtCore, QtGui
  import time
  import sys
  
  class SerialData(QtCore.QObject):
      def __init__(self, message):
          super(SerialData, self).__init__()
          self.__m = message
      def getMsg(self):
          return self.__m
  
  class SerialCon(QtCore.QObject):
  
      finished = QtCore.pyqtSignal()
      received = QtCore.pyqtSignal(SerialData)
  
      def init(self):
         super(SerialCon, self).__init__()
         # TODO setup serial connection:
         # setting up a timer to check periodically for new received serial data
         self.timer = QtCore.QTimer()
         self.timer.setInterval(400)
         self.timer.timeout.connect(self.readData)
         self.timer.start(200)
         # self.finished.emit()
  
      def readData(self):
         self.received.emit(SerialData("New serial data!"))
         print "-&gt; serial.readLine() ..."
  
      @QtCore.pyqtSlot(SerialData)
      def writeData(self, data):
         print "-&gt; serial.write(), ", data.getMsg()
  
  
  class MyGui(QtGui.QWidget):
      serialWrite = QtCore.pyqtSignal(SerialData)
  
      def __init__(self):
         super(MyGui, self).__init__()
         self.initUI()
  
      def initUI(self):
         bSend = QtGui.QPushButton("Send",self)
         bSend.clicked.connect(self.sendData)
         self.show()
  
      @QtCore.pyqtSlot(SerialData)
      def updateData(self, data):
          print "Gui:", data.getMsg()
  
      def sendData(self, pressed):
         data = SerialData("Send me!")
         self.serialWrite.emit(data)
  
  def usingMoveToThread():
      app = QtGui.QApplication(sys.argv)
      guui = MyGui()
  
      thread = QtCore.QThread()
      serialc = SerialCon()
      serialc.moveToThread(thread)
  
      # connecting signals to slots
      serialc.finished.connect(thread.quit)
      guui.serialWrite.connect(serialc.writeData)
      serialc.received.connect(guui.updateData)
      thread.started.connect(serialc.init)
      thread.finished.connect(app.exit)
      thread.start()
  
      sys.exit(app.exec_())
  
  if __name__ == "__main__":
      usingMoveToThread()
  </code></pre>
  
  <p><strong>My Problems:</strong></p>
  
  <ul>
  <li><p>In test code the signal emitted from the <code>SerialCon</code> object (which has
  been moved to the <code>QThread</code>) seems to be not received by the
  corresponding slot  (in <code>MyGui</code>, <code>updateData</code>)</p></li>
  <li><p>Sooner or later the running test code always causes a <code>Segmentation
  fault (core dumped)</code>. Which makes me believe that I missed some
  important bits.</p></li>
  </ul>
  
  <p>What could cause this?</p>
  
  <p>Maybe I m taking a completely wrong approach? -
  so if you have a better idea how to achieve this, I d be very grateful to hear about it!</p>
  
  <p>Thanks a lot!</p>