我在我的程序 的「主」線程中創建了一個QTextEdit組件,然後我啓動另一個線程,它將每隔x秒更新一次QTextEdit,但隨後出現此錯誤:如何在pyQt4中的不同線程中清除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
代碼這最後兩句給我上面提到的錯誤。有人可以給我一個線索如何處理這個問題嗎?
謝謝!
我是這樣做的,得到了: TypeError:pyqtSignal必須綁定到一個QObject,而不是'實例' – RicardoE
您是否確定將它作爲類屬性和** NOT **放入您的'__init__' ? – jdi
是的,最後我發現我的錯誤,我在主類中宣佈了信號......現在它像一種魅力一樣工作,感謝很多朋友,你必須在這裏幫助! – RicardoE