[PyKDE] So how do I pass an argument to a function tied to a callback?

Boudewijn Rempt boud at rempt.xs4all.nl
Sat Mar 3 11:48:32 GMT 2001


On Fri, 2 Mar 2001, Ken Kozman wrote:
 
>
>    Or a little more succinctly, as Aaron said, you want but can't do the
> following:
>
>     self.connect( widget, SIGNAL( "clicked()" ), self.someFunction( num ) )
>
 
I may not be getting the issue, but I vaguely remember wanting somehting
like this, too, a while ago. My solution was a bit different, though.
 
If I under stand this right right, there are two objects, 'self' and
'widget', where 'widget' is owned by 'self', and 'num' is a value known
in 'self'. If you want to connect the pressing of a button (which doesn't
know anything about 'num') to the activation of a function that acts
upon 'num'. In fact, keeping track of num is the 'self''s business. To take
the script Aaron posted earlier, I'd make 'num' a property of the Lambda 
class:

class Lambda(QMainWindow):

    def __init__(self):
        self.num = 1

After that, it's a matter of deciding how you want it to be used. You could
use it directly in changeTopStruct, or have an intermediate slot that emits
a new signal that transports the value (useful if the change of the self.num
is interesting to other classes, too):

....        
        self.connect( self.top_struct_button, SIGNAL( "clicked()" ),
                      self.clicked)
        self.connect( self, PYSIGNAL("sigClicked"), self.changeTopStruct)     
                             
        return # note - you don't need this return at all


    def clicked(self):
      self.emit(PYSIGNAL("sigClicked"), (self.num,))
      
    def changeTopStruct(self, num):
        """
        Pops up an input dialog to prompt user for new top structure name.
        """
....

Of course, since it is possible to emit PYSIGNAL's directly from a connect 
statement, it would be ideal if we could add a value to that too, just as
it is possible to emit a signal with values as in the clicked method above:

        self.connect( self.top_struct_button, SIGNAL( "clicked()" )
                                            , PYSIGNAL("sigClicked")
                                            , (self.num,)
                                            )
                      
But that doesn't work :-(.

-- 

Boudewijn Rempt | http://www.valdyas.org




More information about the PyQt mailing list