Signal not connecting to a method

83 views Asked by At

I am working with sockets. When I receive info from the server I handle it with a method listen that is in a thread. I want to pop up windows from here, so I use signals.

The problem is that the signal does not trigger the function. Here is a working example:

class Client(QtCore.QObject):
    signal = QtCore.pyqtSignal()

    def __init__(self):
        super(Client, self).__init__()
        self.thread_wait_server = threading.Thread(target=self.wait_server)
        self.thread_wait_server.daemon = True
        self.thread_wait_server.start()

    def wait_server(self):
        print('waiting')
        self.signal.emit()
        print("'signal emited")


class Main:
    def Do(self):
        print("'Do' starts")
        self.Launch()
        time.sleep(2)
        print("'Do' ends")

    def Launch(self):
        print("'Launch' starts")
        self.client = Client()
        self.client.signal.connect(self.Tester)
        print("'Launch' ends")

    def Tester(self):
        print("Tester Fired!!")

m = Main()
m.Do()

Tester function is never triggered.

1

There are 1 answers

0
qurban On

The problem with your code is that, you are emitting the signal before connecting it to the slot! Add two print statements like this:

print("connecting the signal")
self.client.signal.connect(self.Tester)
print("signal connected")

You will notice that the signal gets emitted before it gets connected! That's why the slot is not triggering.