title: PyQt5 QThread passing variables to main
body: <p>I am trying to make a GUI which will interact with a serial device. </p>
  
  <p>As of right now, I have successfully made a very simple GUI which can open and close the serial connection.</p>
  
  <p>Below is my code:</p>
  
  <pre><code>import sys
  from PyQt5.QtCore import *
  
  from PyQt5.QtWidgets import QMainWindow, QApplication, QWidget, QMessageBox,      QAction, qApp, QPushButton
  
  from PyQt5.QtGui import QIcon
  
  import serial
  
  ser = serial.Serial()
  ser.baudrate = 115200
  ser.port = 2
  
  class MainUI(QMainWindow):
  
  def __init__(self):
      super().__init__()
      self.initUI()
      self.workerThread = SerialRead()
      self.connect(self.workerThread, QtCore.SIGNAL("mysignal(QString)"),self.on_change, QtCore.Qt.QueuedConnection)
  
  def initUI(self):
      btn1 = QPushButton('Open Serial', self)
      btn1.clicked.connect(self.openSerial)
      btn1.resize(btn1.sizeHint())
      btn1.move(50,50)
  
      btn2 = QPushButton('Close Serial', self)
      btn2.clicked.connect(self.closeSerial)
      btn2.resize(btn2.sizeHint())
      btn2.move(50,100)
  
      exitAction = QAction(QIcon('exit.png'), '&amp;Exit', self)
      exitAction.setShortcut('Alt+F4')
      exitAction.setStatusTip('Exit application')
      exitAction.triggered.connect(self.closeEvent)
  
      self.statusBar()
      menubar = self.menuBar()
      fileMenu = menubar.addMenu('&amp;File')
      fileMenu.addAction(exitAction)
  
      self.statusBar().showMessage('Ready')
      self.setGeometry(300,300,250,150)
      self.setWindowTitle('Motor Driver')
  
      self.show()
  
  def openSerial(self):
      ser.open()
      self.workerThread.start()
  
  def closeSerial(self):
      ser.close()
      self.workerThread.terminate()
  
  def closeEvent(self, event):
      reply = QMessageBox.question(self, 'Message', "Are you sure you want to quit?", QMessageBox.Yes | QMessageBox.No, QMessageBox.No)
      if reply == QMessageBox.Yes:
          event.accept()
      else:
          event.ignore()
  
  class SerialRead(QThread):
  def __init__(self):
      super().__init__()
      self.data = []
  
  def run(self):
      while True:
          x = ser.readline()
          # if x != b'\n':
          #   self.data.append(str(x))
          # else:
          self.emit(QtCore.SIGNAL("mysignal(Qstring)"), (x.decode("utf-8")))
              # self.data = []
  
  
  
  if __name__ == '__main__':
  app = QApplication(sys.argv)
  main = MainUI()
  sys.exit(app.exec_())
  </code></pre>
  
  <p>My question would be on how to pass a variable from my serialRead thread to my MainUI?
  As you can see on my code, I have tried the solutions which I can found online, which is by using:</p>
  
  <pre><code>self.connect(self.workerThread, QtCore.SIGNAL("mysignal(QString)"),self.on_change, QtCore.Qt.QueuedConnection)
  </code></pre>
  
  <p>on my MainUI class and by using:</p>
  
  <pre><code>self.emit(QtCore.SIGNAL("mysignal(Qstring)"), (x.decode("utf-8")))
  </code></pre>
  
  <p>on my thread. </p>
  
  <p>However, this gives me this error:
  'MainUI' object has no attribute 'connect'</p>
  
  <p>So how should I pass a variable from my thread to my MainUI? </p>
  
  <p>It has been almost 2 years since I last touched python and I just started this project about 2 days ago, but is there anyway I can improve my code? Are there any subtle or obvious errors that I made?</p>