Qt中的一个基本规则是,不应从主线程以外的线程更新GUI,这被称为GUI线程。有几种选择,例如通过信号将数据发送到主线程,或使用
QRunnablewith
QThreadPool,如下所示:
码:
qtCreatorFile = "gui.ui" Ui_MainWindow, QtbaseClass = uic.loadUiType(qtCreatorFile)estudiantes = [' ',' ',' ',' ']class SerialRunnable(QtCore.QRunnable): def __init__(self, w): QtCore.QRunnable.__init__(self) self.w = w self.ser = serial.Serial('COM9', baudrate=9600, timeout=1) def run(self): while True: dato = self.ser.readline().depre('ascii') if dato != "": QtCore.QmetaObject.invokeMethod(self.w, "setValues", QtCore.Qt.QueuedConnection, QtCore.Q_ARG(str, dato)) QtCore.QThread.msleep(10)class MyApp(QtWidgets.QMainWindow, Ui_MainWindow): def __init__(self): QtWidgets.QMainWindow.__init__(self) self.setupUi(self) runnable = SerialRunnable(self) QtCore.QThreadPool.globalInstance().start(runnable) @QtCore.pyqtSlot(str) def setValues(self, dato): estudiantes.insert(0,dato) estudiantes.pop() self.Box1.setText(estudiantes[0]) self.Box2.setText(estudiantes[1]) self.Box3.setText(estudiantes[2]) self.Box4.setText(estudiantes[3])
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)