我在使用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'