我在PyQt中的线程之间进行通信时遇到了一些问题。我正在使用信号在两个线程之间进行通信,即发件人和听众。发件人发送消息,这些消息有望由收听者接收。但是,不会收到任何消息。任何人都可以提出什么可能会出错?我确信它一定是简单的,但我一直在四处寻找,没有发现任何东西。提前致谢!PyQt中的线程和信号问题
from PyQt4 import QtCore,QtGui
import time
class Listener(QtCore.QThread):
def __init__(self):
super(Listener,self).__init__()
def run(self):
# just stay alive, waiting for messages
print 'Listener started'
while True:
print '...'
time.sleep(2)
def say_hello(self):
print ' --> Receiver: Hello World!'
class Sender(QtCore.QThread):
# a signal with no arguments
signal = QtCore.pyqtSignal()
def __init__(self):
super(Sender,self).__init__()
# create and start a listener
self.listener = Listener()
self.listener.start()
# connect up the signal
self.signal.connect(self.listener.say_hello)
# start this thread
self.start()
def run(self):
print 'Sender starting'
# send five signals
for i in range(5):
print 'Sender -->'
self.signal.emit()
time.sleep(2)
# the sender's work is done
print 'Sender finished'
Qt的决定发出一个信号,该线程取决于哪个线程创建的对象实例的信号。如果你从你的主线程创建一个监听对象并将信号发送给它的含义,它会调用它们在主线程中。请记住,该对象不是线程。见例如ftp://ftp.qt.nokia.com/videos/DevDays2007/DevDays2007%20-%20Multi-threading_in_Qt.pdf,详细了解Qt中的“线程关联”。 – 2010-12-01 14:10:18