Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Thread-to-thread signals not working #155

Open
Ruuttu opened this issue Mar 28, 2017 · 1 comment
Open

Thread-to-thread signals not working #155

Ruuttu opened this issue Mar 28, 2017 · 1 comment

Comments

@Ruuttu
Copy link

Ruuttu commented Mar 28, 2017

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 )
@ohofherr
Copy link

ohofherr commented Dec 5, 2024

has this ever been resolved? I have a similar problem. As soon as I move my QObject into another thread the signals are not working

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants