[PyKDE] Lambda function call in connect statement

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


Phil Thompson wrote:

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

I've attached a script that displays the core dump.  I also included the
non-lambda version of the connect statement that shows how the pop-up is
created prior to the construction of the main window.

Aaron

-- 
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
-------------- next part --------------
#!/usr/bin/env python
from qt import *
import os
import sys

class Lambda(QMainWindow):

    def __init__(self):

        num = 1

        QMainWindow.__init__(self)

        # Build the menubar.
        mb = self.menuBar()
        
        file_menu = QPopupMenu(self)
        mb.insertItem('&File',file_menu)

        help_menu = QPopupMenu(self)
        mb.insertItem('&Help',help_menu)

        # Build the middle VBox where widgets will go.
        self.vb = QVBox( self )
        self.setCentralWidget( self.vb )
        self.vb.setFrameStyle(QFrame.StyledPanel|QFrame.Raised)
        self.vb.setMargin(1)
        self.vb.setLineWidth(2)

        self.top_struct_hb = QHBox( self.vb )
        self.top_struct_value = QLabel( 'Test', self.top_struct_hb)
        self.top_struct_button = QPushButton('Click me',
                                             self.top_struct_hb)

        # -----------------------------------------------------------
        # Uncomment these lines to see the effect without the lambda
        # statement. The QInputDialog command will be created before
        # the main window is created.
        # -----------------------------------------------------------
        
##        self.connect( self.top_struct_button, SIGNAL( "clicked()" ),
##                      self.changeTopStruct(num))
        self.connect( self.top_struct_button, SIGNAL( "clicked()" ),
                      lambda s=self,x=num:s.changeTopStruct(x))
                                        
                      
        # Build the status bar.
        sb = self.statusBar()

        return


    def changeTopStruct(self, a_num):
        """
        Pops up an input dialog to prompt user for new top structure name.
        """

        text_tuple = QInputDialog.getText( 'Test', 'Test', None, None )

        if text_tuple[1] is 1 and text_tuple[0] is not None:
            print 'Ok'
        else:
            print 'Cancel'
            return

# main()
a = QApplication( sys.argv )

a_lambda = Lambda()
a.setMainWidget( a_lambda )
a_lambda.show()

a.exec_loop()


More information about the PyQt mailing list