(pyqt) Parameters when connecting a signal to a method?

Discussion in 'Python' started by exhuma.twn, Sep 24, 2007.

  1. exhuma.twn

    exhuma.twn Guest

    Some code:

    ----------------------------------------------------------------------

    def foobar(w):
    print w

    QtCore.QObject,connect( my_line_edit,
    QtCore.SIGNAL("returnPressed()"), foobar )

    ----------------------------------------------------------------------


    How can I get this to work so "foobar" prints out the sender of the
    signal (i.e. my_line_edit)?
     
    exhuma.twn, Sep 24, 2007
    #1
    1. Advertising

  2. exhuma.twn wrote:

    > Some code:
    >
    > ----------------------------------------------------------------------
    >
    > def foobar(w):
    > print w
    >
    > QtCore.QObject,connect( my_line_edit,
    > QtCore.SIGNAL("returnPressed()"), foobar )
    >
    > ----------------------------------------------------------------------
    >
    >
    > How can I get this to work so "foobar" prints out the sender of the
    > signal (i.e. my_line_edit)?


    I _think_ there is a way to get the signals sender in Qt itself. But I'm
    unsure how to get that.

    Alternatively, you can use a closure to create a reference:

    def foobar(source, w):
    print w

    def slotgen(source, slot):
    def _slot(*args):
    return slot(*((source,) + args))
    return _slot

    my_slot = slotgen(my_line_edit, foobar)

    QtCore.QObject,connect( my_line_edit,
    QtCore.SIGNAL("returnPressed()"), my_slot )

    However, be careful to keep a reference to my_slot around! Otherwise, it
    will be garbage collected!

    diez
     
    Diez B. Roggisch, Sep 24, 2007
    #2
    1. Advertising

  3. exhuma.twn

    exhuma.twn Guest

    On Sep 24, 4:47 pm, "Diez B. Roggisch" <> wrote:
    > exhuma.twn wrote:
    > > Some code:

    >
    > > ----------------------------------------------------------------------

    >
    > > def foobar(w):
    > > print w

    >
    > > QtCore.QObject,connect( my_line_edit,
    > > QtCore.SIGNAL("returnPressed()"), foobar )

    >
    > > ----------------------------------------------------------------------

    >
    > > How can I get this to work so "foobar" prints out the sender of the
    > > signal (i.e. my_line_edit)?

    >
    > I _think_ there is a way to get the signals sender in Qt itself. But I'm
    > unsure how to get that.
    >
    > Alternatively, you can use a closure to create a reference:
    >
    > def foobar(source, w):
    > print w
    >
    > def slotgen(source, slot):
    > def _slot(*args):
    > return slot(*((source,) + args))
    > return _slot
    >
    > my_slot = slotgen(my_line_edit, foobar)
    >
    > QtCore.QObject,connect( my_line_edit,
    > QtCore.SIGNAL("returnPressed()"), my_slot )
    >
    > However, be careful to keep a reference to my_slot around! Otherwise, it
    > will be garbage collected!
    >
    > diez


    Thanks diez. This works :)
    Although, I still have to digest *what* this closure does, but I will
    leave this as an exercise to myself. ;)
     
    exhuma.twn, Sep 24, 2007
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. ulysses
    Replies:
    4
    Views:
    768
    Werner Schiendl
    Oct 22, 2003
  2. Alexander Eisenhuth
    Replies:
    2
    Views:
    868
    Alexander Eisenhuth
    May 29, 2007
  3. Alexander Eisenhuth

    PyQt disconnect signal / slot

    Alexander Eisenhuth, Sep 25, 2007, in forum: Python
    Replies:
    2
    Views:
    1,277
    Alexander Eisenhuth
    Sep 25, 2007
  4. pallav
    Replies:
    2
    Views:
    902
    pallav
    May 3, 2008
  5. Replies:
    2
    Views:
    98
Loading...

Share This Page