I have developed a multithreaded gui that reads serial data in a separate thread. I am very new to threading, pyqt, python in general. I used this site as a reference to get this far and its working properly but researching how to add a second thread I discovered a number of articles and posts that you shouldn't subclass threads. How would I go about converting this to the "proper" method?
class AThread(QtCore.QThread):
updated = QtCore.pyqtSignal(str)
query = QtCore.pyqtSignal(str)
def __init__(self):
QtCore.QThread.__init__(self)
def run(self):
try:
while True:
if ser.in_waiting:
line=ser.readline()[:-2]#remove end of line \r\n
self.updated.emit(line.decode('utf-8'))
if main_window.keywordCheckBox.isChecked():
if main_window.keywordInput.text() in line.decode('utf-8'):
self.query.emit("Match!")
self.query.emit(line.decode('utf-8'))
except serial.serialutil.SerialException:
pass
class MyMainWindow(QtGui.QMainWindow):
def __init__(self, parent=None):
self.thread= AThread()
self.thread.updated.connect(self.updateText)
self.thread.query.connect(self.matchFound)
Here is an article from the Qt documentation that you may find helpful http://doc.qt.io/qt-4.8/thread-basics.html
There is a paragraph named "Which Qt Thread Technology Should You Use?" conataining a table which suggests what approach to use depending on what you want to achieve
Possibly in your case you might need to follow one of the approaches described in the last two rows of the table.
If this is the case then your code should look like this