python - Threading and Signals problem in PyQt -
i'm having problems communicating between threads in pyqt. i'm using signals communicate between 2 threads, sender , listener. sender sends messages, expected received listener. however, no messages receieved. can suggest might going wrong? i'm sure must simple, i've been looking around hours , not found anything. in advance!
from pyqt4 import qtcore,qtgui import time class listener(qtcore.qthread): def __init__(self): super(listener,self).__init__() def run(self): # stay alive, waiting messages print 'listener started' while true: print '...' time.sleep(2) def say_hello(self): print ' --> receiver: hello world!' class sender(qtcore.qthread): # signal no arguments signal = qtcore.pyqtsignal() def __init__(self): super(sender,self).__init__() # create , start listener self.listener = listener() self.listener.start() # connect signal self.signal.connect(self.listener.say_hello) # start thread self.start() def run(self): print 'sender starting' # send 5 signals in range(5): print 'sender -->' self.signal.emit() time.sleep(2) # sender's work done print 'sender finished'
i'm not sure if need, works fine...
from pyqt4 import qtcore,qtgui import time class listener(qtcore.qthread): def __init__(self): super(listener,self).__init__() def run(self): print('listener: started') while true: time.sleep(2) def connect_slots(self, sender): self.connect(sender, qtcore.signal('testsignal'), self.say_hello) def say_hello(self): print('listener: received signal') class sender(qtcore.qthread): def __init__(self): super(sender,self).__init__() def run(self): in range(5): print('sender: sending signal') self.emit(qtcore.signal('testsignal')) time.sleep(2) print('sender: finished') if __name__ == '__main__': o_qapplication = qtgui.qapplication([]) my_listener = listener() my_sender = sender() my_listener.connect_slots(my_sender) my_listener.start() my_sender.start() i_out = o_qapplication.exec_()
Comments
Post a Comment