Results 1 to 3 of 3

Thread: Passing values to custom 'slot' functions (pyqt)

  1. #1
    Join Date
    Sep 2009
    Posts
    7
    Thanks
    3
    Qt products
    Platforms
    Unix/X11 Windows

    Default Passing values to custom 'slot' functions (pyqt)

    Hi,

    Can I first say thanks to all who read my newb questions and helped me out, much appreciated.

    Quick pyqt question:

    mywindow has a button on it. I want to run the clicked_function when clicked. This works
    Qt Code:
    1. QtCore.QObject.connect(mywindow.somebutton, QtCore.SIGNAL("clicked()"), clicked_function)
    To copy to clipboard, switch view to plain text mode 


    But if I want to click the button and pass 'foo' to the clicked_function, I thought I would be able to use
    Qt Code:
    1. QtCore.QObject.connect(mywindow.somebutton, QtCore.SIGNAL("clicked()"), clicked_function('foo'))
    To copy to clipboard, switch view to plain text mode 


    This brings up an 'argument of incorrect type' error. So how can I pass a value to the function? I thought I might try...

    Qt Code:
    1. QtCore.QObject.connect(mywindow.somebutton, QtCore.SIGNAL("clicked()"), pre_clicked_function)
    2.  
    3. def pre_clicked_function():
    4. clicked_function('foo')
    To copy to clipboard, switch view to plain text mode 

    But this is far from streamlined! Any better ideas?

  2. #2
    Join Date
    Sep 2008
    Posts
    18
    Thanks
    2
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Passing values to custom 'slot' functions (pyqt)

    Hi!

    Use functools.partial, it can take a function and "pre-fill" some or all its arguments, like this:
    Qt Code:
    1. import functools
    2. #...
    3. self.connect(mywindow.somebutton, SIGNAL("clicked()"), functools.partial(clicked_function, "foo"))
    To copy to clipboard, switch view to plain text mode 

  3. The following user says thank you to hkvm for this useful post:

    Richie (6th September 2009)

  4. #3
    Join Date
    Dec 2007
    Posts
    2
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Passing values to custom 'slot' functions (pyqt)

    Hi,

    Another way to do this would be to use lambda:

    Qt Code:
    1. self.connect(mywindow.somebutton, SIGNAL("clicked()"), (lambda p="foo" : clicked_function(p)))
    To copy to clipboard, switch view to plain text mode 

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.