Search code examples
pythonpyqtsignalsdisconnectslot

pyqt disconnect slots. New style


I assign a slot with this.

...
self.query = QtGui.QLineEdit(self)            
self.query.textChanged.connect(lambda: self.makeThread(self.googleSearch()))
self.query.returnPressed.connect(lambda: self.launchNavigator(1))
...

but how can I disconnect? I tried with this but it does not work...

self.query.textChanged.disconnect(lambda: self.makeThread(self.googleSearch()))
self.query.returnPressed.disconnect(lambda: self.launchNavigator(1))

Solution

  • The lambda expressions return different functions that (more or less by chance ;) ) will do the same thing. Therefore, what you connected your signal to is not the same thing as the second lambda you're using when trying to disconnect it; see this example:

    >>> f = lambda x: x
    >>> g = lambda x: x
    >>> f is g
    False
    

    You can either use self.query.textChanged.disconnect() without any parameters, which will disconnect the signal from all slots (which might be ok if you only have that one connection) or you will have to store a reference to the lambda somewhere:

    self.func1 = lambda: self.makeThread(self.googleSearch())
    self.query.textChanged.connect(self.func1)
    ...
    self.query.textChanged.disconnect(self.func1)