-
Notifications
You must be signed in to change notification settings - Fork 64
Open
Description
Signals are not getting through if sender and receiver are in different threads.
import time
from PySide.QtCore import *
from PySide.QtGui import *
class NoiseMaker(QObject):
noise = Signal(int)
def work(self):
while True:
print "Making some noise"
self.noise.emit( 100 )
time.sleep(0.5)
class Listener(QObject):
def work(self):
while True:
time.sleep(0.5)
def receiveNoise(self, integer):
print "Got some noise"
class App(QApplication):
def __init__(self):
super( App, self ).__init__([])
# Create workers
self.noiseMaker = NoiseMaker()
self.listener = Listener()
# Create threads
self.noiseMakerThread = QThread()
self.listenerThread = QThread()
# Move workers to threads
self.noiseMaker.moveToThread( self.noiseMakerThread )
self.listener.moveToThread( self.listenerThread )
# NoiseMaker emits to Listener
self.noiseMaker.noise.connect( self.listener.receiveNoise )
# Start noise thread
self.noiseMakerThread.started.connect( self.noiseMaker.work )
self.noiseMakerThread.start()
# Start listener thread
self.listenerThread.started.connect( self.listener.work )
self.listenerThread.start()
App().exec_()
Expected output:
Making some noise
Got some noise
Actual output:
Making some noise
However:
If you comment out these lines the example works:
self.noiseMaker.moveToThread( self.noiseMakerThread )
self.listener.moveToThread( self.listenerThread )
Alternatively, if you move both workers to the same thread, that also works:
self.noiseMaker.moveToThread( self.noiseMakerThread )
self.listener.moveToThread( self.noiseMakerThread )
Metadata
Metadata
Assignees
Labels
No labels