[PyKDE] Lambda function call in connect statement

Aaron J. Ginn aaron.ginn at motorola.com
Thu Mar 1 21:12:21 GMT 2001

Phil Thompson wrote:

> As lambda functions are supposed to be "syntactic sugar for a normal
> function definition" I don't see how using it helps you. Your function
> wouldn't be executed until the button click anyway.

If I write the connect statement without a lambda as follows:

         self.connect( self.top_struct_button, SIGNAL( "clicked()" ),

where changeTopStruct is

    def changeTopStruct(self, obj):
        Pops up an input dialog to prompt user for new top structure

        ok_text_tuple = QInputDialog.getText( 'Change Top Structure',
                                              'Enter new Top Structure',

        if ok_text_tuple[1] is 1 and ok_text_tuple[0] is not None:
            obj.set_topstruct( ok_text_tuple[0] )
            print obj.get_topstruct()
            self.top_struct_value.setText( obj.get_topstruct() )

The QInputDialog widget in the called function is created before my top
QMainWindow that calls the function.  As I recall from my work with
Tkinter, this is because the call from the connect statement is executed
immediately due to the parentheses in the self.changeTopStruct(ver_obj)
argument.  I thought the only way to get around this is to use a lambda
function such as

        self.connect( self.top_struct_button, SIGNAL( "clicked()" ),
                      lambda s=self,o=ver_obj:s.changeTopStruct(o))

This suppresses the creation of the QInputDialog box until the button is
pressed.  Is there a better way to do this instead of using a lambda?

> However, I don't like segfaults - send me a small but complete script
> that demonstrates the problem and I'll take a look.
> Phil

Will do, but it will take me a little time to put something together.

Aaron J. Ginn                    Phone: 480-814-4463
Motorola SemiCustom Solutions    Pager: 877-586-2318
1300 N. Alma School Rd.          Fax  : 480-814-4463
Chandler, AZ 85226 M/D CH260     mailto:aaron.ginn at motorola.com

More information about the PyQt mailing list