I want to execute a function in a list of objects, and store the results. My problem in the code below, is that although I try to disconnect all signals, functionC keeps triggering somehow continuously. (disconnect outputs False)
class object(...):
def init(self):
...
self.emit(SIGNAL('result(bool)', result)
...
class classA(...):
def functionA(self):
self.results=[]
self.i=0
self.object[...]=... # len(self.object)=n
self.functionB()
def functionB(self):
self.connect(self.object[self.i], SIGNAL('result(bool)'), lambda bool: self.functionC(bool))
self.object[self.i].init()
def functionC(self, result):
print self.disconnect(self.object[self.i], SIGNAL('result(bool)', lambda bool: self.functionC(bool)) # outputs False (why cannot I disconnect it?)
self.i+=1
self.results.append(result)
if self.i==len(self.object):
print "finish"
self.i=0
return
self.functionB()
Anyone could imagine the problem? Thanks
I'm not sure if your code is just an example or if you really are using lambda functions for your slot. If you are indeed using a lambda, the reason is most likely because the lambda functions are completely different objects between your connect and disconnect:
>>> fn1 = lambda bool: self.functionC(bool)
>>> fn2 = lambda bool: self.functionC(bool)
>>> print fn1, fn2
<function <lambda> at 0x1004a9320> <function <lambda> at 0x1004a9398>
>>> fn1 == fn2
False
>>> fn1 is fn2
False
If pyqt is storing the function references and looking for the one you specify to disconnect, it will report that its not connected. What you want to do is actually reference a persistant callback. Either store the lambda as a member attribute and reference the same one both times, or define it as a standard method or function.