[英]how to clear QTextEdit in different thread in pyQt4
我有一個QTextEdit組件在我的程序的“主”線程中創建然后我啟動另一個線程,它將每隔x秒更新此QTextEdit,但后來我得到此錯誤:
QObject: Cannot create children for a parent that is in a different thread.
這就是我這樣做的方式:
def initGui():
#some gui components
global txt_list
txt_list = QtGui.QTextEdit(w)
txt_list.resize(580,400)
txt_list.move(50, 50)
txt_list.setReadOnly(1)
txt_list.setFont(font_consolas)
#more gui components
def update_list():
t_monitor = threading.Thread(target=monitor_vector)
t_monitor.daemon = True
t_monitor.setName('monitor')
t_monitor.start()
def monitor_vector():
#retrieve info...
lock = threading.Lock
lock = True
txt_list.clear() #clear list, to set the new one
txt_list.setText('updated list')
lock = False
這最后兩行代碼給出了上面提到的錯誤。 有人能給我一個如何處理這個問題的線索嗎?
謝謝!
關於Qt的一個主要問題是你不能從主GUI線程以外的任何線程調用任何QWidget方法。 所有的通信都必須通過從額外線程發出信號來完成,這些信號將轉發到主gui。
首先,我看到你正在使用全局變量,並且缺少self
關鍵字,所以我假設你沒有使用類。 我的例子也將包含一個類示例。
這個例子很簡單,就像你的一樣,概述了你可能想要的方向:
from PyQt4 import QtGui, QtCore
class MyWidget(QtGui.QWidget):
def __init__(self, parent=None):
super(MyWidget, self).__init__(parent)
...
self.txt_list = QtGui.QTextEdit(self)
self.txt_list.resize(580,400)
self.txt_list.move(50, 50)
self.txt_list.setReadOnly(1)
self.txt_list.setFont(font_consolas)
...
self.monitor = Monitor()
self.monitor.updateText.connect(self._handleTextUpdate)
self.monitor.update_list()
def _handleTextUpdate(self, txt):
self.txt_list.clear()
self.txt_list.setText(txt)
class Monitor(QtCore.QObject):
updateText = QtCore.pyqtSignal(str)
def update_list(self):
t_monitor = Thread(self.monitor_vector, parent=self)
t_monitor.daemon = True
t_monitor.setName('monitor')
t_monitor.start()
def monitor_vector(self):
...
self.updateText.emit('updated list')
class Thread(QtCore.QThread):
def __init__(self, fn, args, kwargs, parent=None):
super(Thread, self).__init__(parent)
self._fn = fn
self._args = args
self._kwargs = kwargs
def run(self):
self._fn(*self._args, **self._kwargs)
需要注意的主要是線程中運行的函數是發出信號。 他們不了解其他課程中的QWidgets。 您的MyWidget
類將信號連接到可以更新QLineEdit的插槽。 當線程發出信號時,它將排隊到主線程並由接收槽執行。
我還創建了一個簡單的QThread子類,它可以接受一個函數和args,復制標准的lib Thread類的工作方式。 您應該堅持使用QThread,因為它是一個QObject子類,並且支持信號並且可以運行事件循環。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.