[PyQt] signals and slots

Giovanni Bajo rasky at develer.com
Fri Jul 18 09:02:18 BST 2008


On Thu, 2008-07-17 at 11:20 +0200, Andreas Pakulat wrote:
> On 17.07.08 09:54:40, Strato wrote:
> > Hi Folks,
> >
> > Thanks Russel for your answer, but I'm not able to figure out how my  
> > argument can be the correct one that way... Maybe I'm not as skilled as  
> > I need to realy understand hot this works...
> >
> > hum seems that your style is in pure Qt, but I'm writing PyQt.
> >
> > so, maybe an example will be the best way to explain my needs:
> >
> > I have a popup menu, with entry generated by iteration, each entry is a  
> > submenu, and all sub menus are the same entry.
> >
> > I can use QAction and its 'activated()' signal when I click on an item  
> > of a sub-menu, but I need to know which of the sub-menu is activated, to  
> > call my slot with the correct argument(s).
> 
> Look at the QSignalMapper class, it does exactly what you want. Connect
> the actions 'activated' signal to the mapper's 'map' slot, and its
> 'mapped(int)' signal to your own slot. Then use
> 'setMapping(action,somenumber)' to activate the mapping.

Or you can simply use lambdas as slots:

for idx, foobar in enumerate(....):
    action = makeQAction(foobar)
    QObject.connect(action, SIGNAL("activated()"),
                    lambda idx=idx: self.activated(idx))
    menu.addAction(action)

[...]

def activated(self, idx):
    """Called when the menu items #idx of the menu has been
activated."""
    print idx


-- 
Giovanni Bajo
Develer S.r.l.
http://www.develer.com




More information about the PyQt mailing list