[prev in list] [next in list] [prev in thread] [next in thread] 

List:       pykde
Subject:    Re: [PyKDE] PyQt and lambda expressions doesn't like each other
From:       Hans-Peter Jansen <hpj () urpla ! net>
Date:       2003-04-28 16:12:14
[Download RAW message or body]

On Monday 28 April 2003 16:42, Phil Thompson wrote:
> On Monday 28 April 2003 2:12 pm, Hans-Peter Jansen wrote:
> > On Monday 28 April 2003 14:53, Phil Thompson wrote:
> > > On Monday 28 April 2003 1:31 pm, Hans-Peter Jansen wrote:
> > > > This doesn't work (while it should):
> > > >
> > > > import sys
> > > > from qt import *
> > > >
> > > > app = QApplication(sys.argv)
> > > > button = QPushButton("&Hello World", None)
> > > > app.setMainWidget(button)
> > > > button.show()
> > > > app.connect(button, SIGNAL("clicked()"), lambda: app.exit(0))
> > > > app.exec_loop()
> > > >
> > > > Any ideas, why?
> > >
> > > connect() doesn't take a reference to a slot so it gets garbage
> > > collected immediately.
> >
> > This leaves the question, what are the consequences, if it would?
>
> A very high chance of circular references and, consequently, widgets
> that refuse to disappear.
>
> That was the original behaviour, many years ago.

How about adding this to the examples section for future reference:

["qtlambda.py" (text/x-python)]

#!/usr/bin/env python
#
# Want to use lambda expressions as slots with PyQt? 
# Or you have tried and it wasn't working as expected?
# 
# Read below

import sys
from qt import *

app = QApplication(sys.argv)
button = QPushButton("&Hello World", None)
app.setMainWidget(button)
button.show()
# Will not work:
#app.connect(button, SIGNAL("clicked()"), lambda: app.exit(0))
# Remember to keep a reference to such an expression
# otherwise it gets garbage collected immediately
# without any further notice. 
# Will work:
appdone = lambda: app.exit(0)
app.connect(button, SIGNAL("clicked()"), appdone)
# The reason for this behaviour is to avoid circular references,
# which could result in widgets, that refuse to disappear.
app.exec_loop()


_______________________________________________
PyKDE mailing list    PyKDE@mats.gmd.de
http://mats.gmd.de/mailman/listinfo/pykde

[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic