I would like to add a caveat/extension to @rainer's answer pertaining to bound methods. Keep in mind that binding a method to an instance (e.g., by writing self.slotname
) actually creates a new closure every time (as in this question).
You would therefore have the same problem doing
def test_slot(self):
self.makeThread(self.googleSearch())
...
self.query.textChanged.connect(self.test_slot)
...
self.query.textChanged.disconnect(self.test_slot)
as you did with the original lambda
in your question. The solution is to store the closure self.test_slot
in an attribute, as @rainer suggests. This is necessary because a new object that is roughly equivalent to lambda: type(self).test_slot(self)
is generated every time you write self.test_slot
in your code. The following will work fine as long as you do the book-keeping accurately:
self.func = self.test_slot
self.query.textChanged.connect(self.func)
...
self.query.textChanged.disconnect(self.func)
functools.partial
as well? – Phanerogam