正确使用 Qthread 子类化有效,更好的方法?
Proper use of Qthread subclassing works, better method?
我开发了一个多线程图形用户界面,可以在单独的线程中读取串行数据。一般来说,我对线程、pyqt、python 很陌生。我使用这个网站作为参考来达到这个目的并且它正常工作但是研究如何添加第二个线程我发现了一些你不应该将线程子类化的文章和帖子。我将如何将其转换为 "proper" 方法?
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)
这是 Qt 文档中的一篇文章,您可能会觉得有帮助
http://doc.qt.io/qt-4.8/thread-basics.html
有一个名为 "Which Qt Thread Technology Should You Use?" 的段落包含一个 table,它根据您想要实现的目标建议使用哪种方法
可能在您的情况下,您可能需要遵循 table.
最后两行中描述的方法之一
如果是这种情况,那么您的代码应该如下所示
class AWorker(QObject):
#define your signals here
def __init__(self):
super(AWorker, self).__init__()
def myfunction(self):
#Your code from run function goes here.
#Possibly instead of using True in your while loop you might
#better use a flag e.g while self.running:
#so that when you want to exit the application you can
#set the flag explicitly to false, and let the thread terminate
class MainWindow(...)
def __init__(...)
...
self.worker = AWorker()
self.thread = QThread()
self.worker.moveToThread(self.thread)
self.thread.started.connect(self.worker.myfunction)
#Now you can connect the signals of AWorker class to any slots here
#Also you might want to connect the finished signal of the thread
# to some slot in order to perform something when the thread
#finishes
#e.g,
self.thread.finished.connect(self.mythreadhasfinished)
#Finally you need to start the thread
self.thread.start()
我开发了一个多线程图形用户界面,可以在单独的线程中读取串行数据。一般来说,我对线程、pyqt、python 很陌生。我使用这个网站作为参考来达到这个目的并且它正常工作但是研究如何添加第二个线程我发现了一些你不应该将线程子类化的文章和帖子。我将如何将其转换为 "proper" 方法?
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)
这是 Qt 文档中的一篇文章,您可能会觉得有帮助 http://doc.qt.io/qt-4.8/thread-basics.html
有一个名为 "Which Qt Thread Technology Should You Use?" 的段落包含一个 table,它根据您想要实现的目标建议使用哪种方法
可能在您的情况下,您可能需要遵循 table.
最后两行中描述的方法之一如果是这种情况,那么您的代码应该如下所示
class AWorker(QObject):
#define your signals here
def __init__(self):
super(AWorker, self).__init__()
def myfunction(self):
#Your code from run function goes here.
#Possibly instead of using True in your while loop you might
#better use a flag e.g while self.running:
#so that when you want to exit the application you can
#set the flag explicitly to false, and let the thread terminate
class MainWindow(...)
def __init__(...)
...
self.worker = AWorker()
self.thread = QThread()
self.worker.moveToThread(self.thread)
self.thread.started.connect(self.worker.myfunction)
#Now you can connect the signals of AWorker class to any slots here
#Also you might want to connect the finished signal of the thread
# to some slot in order to perform something when the thread
#finishes
#e.g,
self.thread.finished.connect(self.mythreadhasfinished)
#Finally you need to start the thread
self.thread.start()