Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

PySide Signals not being sent to Slot, from QThread object.



  • Hello,

    I'm working with a multi-threading application, where a worker thread gets created, that emits a signal.
    After creating the thread, I connect the signal with an object slot, that will perform some action.

    The problem, is the object slot, is not called, can someone help to figure out what's wrong ?

    import time
    from PySide import QtCore
    from PySide.QtCore import Slot, Signal
    
    
    class Worker1(QtCore.QThread):
    
        task_done_signal = Signal(int)
    
        def __init__(self):
            super(Worker1, self).__init__()
            self._run = False
    
        def run(self):
            self._loop()
    
        def _loop(self):
            count = 0
            while self._run:
                print("running")
                count += 1
                self.task_done_signal.emit(count)
    
        def start(self):
            self._run = True
            super(Worker1, self).start()
    
        def stop(self):
            self._run = False
    
    
    class Worker1Listener(QtCore.QObject):
        def __init__(self):
            super(Worker1Listener, self).__init__()
    
        @Slot()
        def print_task(self, val):
            print("listener: {}".format(val))
    
    
    def test_signals_and_threads():
    
        # create the thread
        worker = Worker1()
    
        # create the listener
        listener = Worker1Listener()
    
        # connect the thread signal with the slot
        worker.task_done_signal.connect(listener.print_task)
    
        worker.start()
    
        time.sleep(5)
    
        worker.stop()
    
        time.sleep(5)
    
    
    if __name__ == '__main__':
        test_signals_and_threads()
    

  • Lifetime Qt Champion

    Hi and welcome to devnet,

    There's no event loop running at all. Therefore signals and slots won't be working.


Log in to reply