pyside / PySide

ATTENTION: This project is deprecated, please refer to PySide2
https://wiki.qt.io/PySide2
GNU Lesser General Public License v2.1
291 stars 66 forks source link

Thread-to-thread signals not working #155

Open Ruuttu opened 7 years ago

Ruuttu commented 7 years ago

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 )