[PyKDE] PyQt not refing slot methods - bug?

Phil Thompson phil at river-bank.demon.co.uk
Fri Aug 30 17:32:00 BST 2002


Burley, Brent wrote:

> I was trying to use a lambda method as a slot method like this:
> 
> import sys           
> from qt import *
> def hello(): print 'hello'
> app = QApplication(sys.argv)
> b = QPushButton('hello', None)
> QObject.connect(b, SIGNAL('clicked()'), lambda: hello())
> app.setMainWidget(b)
> b.show()
> app.exec_loop()
> 
> 
> The lambda method never gets called.  It appears that PyQt doesn't ref slot methods so the lambda method gets deleted before it can be used.  If I change the code to keep a reference to the lambda, it works fine:
> 
> l = lambda: hello()
> QObject.connect(b, SIGNAL('clicked()'), l)
> 
> 
> Is it a bug that PyQt isn't refing slot methods?  Doesn't anyone use lambdas for slots?


It's not a bug, it's designed that way so that you don't need to 
explicitly disconnect signals when you want to delete an object.

Phil




More information about the PyQt mailing list