[PyKDE] menu/actions questions

Garry Hodgson garry at sage.att.com
Wed Feb 12 14:53:00 GMT 2003


i'm trying to create a menu of mail folders for a mail client
i'm writing.  results so far are good, but i'm a bit mystified 
about how to get menus to do what i want.  so, a couple of 
questions:

1.
in the following code, i create two menu items, "Test 1" and "Test 2".
i insert them into a menu, attaching a function to each.  if i run the
app,
test 1 works, but test 2 silently fails.  no error, just no action.
if i assign the lambda expression to a var, and run it, it does what
i'd expect.  but apparently some magic is happening behind the scenes.

i've tried various combinations of args/no args, explicitly creating
QAction objects and using addTo to attach them to the menu, etc.

can someone explain what i'm missing?

2.
maybe i'm just way off base, and there's a better way to do this.
what i want to accomplish is to build, on the fly, a menu tree of
my mail folders.  when selected, i want it call a method of mine
with something i can use to tell what was selected.

any help would be greatly appreciated.

thanks

------------------------------------------------------
#!/usr/bin/env python2

import sys
from qt import *

class MainWindow( QMainWindow ):
    def __init__( self, parent=None, name=None ):
        QMainWindow.__init__( self, parent, name )
        self.CreateMenuBar()

    def CreateMenuBar( self ):
        fileMenu = QPopupMenu( self )

        fileMenu.insertItem( 'Test 1', self.test1 )
        fileMenu.insertItem( 'Test 2', lambda: self.test2( 'hi there' )
)

        self.menuBar().insertItem( 'File', fileMenu )

    def test1( self ):
        print 'test 1 fired'

    def test2( self, msg ):
        print 'test 2 says', msg

def main():
    app = QApplication( sys.argv )
    mainWin = MainWindow()
    app.setMainWidget( mainWin )
    mainWin.show()
    app.exec_loop()

main()

-- 
Garry Hodgson                   Those who would give up essential
liberty
Senior Hacker                   to purchase a little temporary safety
Software Innovation Services    deserve neither liberty nor safety.
AT&T Labs                       
garry at sage.att.com                 - Benjamin Franklin, 1775




More information about the PyQt mailing list