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))
+5
source share
2 answers

Lambda expressions return different functions that (more or less randomly;)) will do the same. Therefore, what you connected your signal to is not the same as the second lambda that you use when you try to disconnect; see this example:

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

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

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

/ @rainer, . , (, self.slotname) ( ).

, :

def test_slot(self):
    self.makeThread(self.googleSearch())

...

    self.query.textChanged.connect(self.test_slot)

...

    self.query.textChanged.disconnect(self.test_slot)

lambda . , self.test_slot , @rainer. , , lambda: type(self).test_slot(self), , self.test_slot . , :

    self.func = self.test_slot
    self.query.textChanged.connect(self.func)

...

    self.query.textChanged.disconnect(self.func)
0

All Articles