views:

135

answers:

1

Hello,

I have a couple of checkboxes on my form, and I don't want to write separate event handler for each, because they all will implement the same logic. Instead I want to write just one event handler that will know about what checkbox has been clicked.

E.g. in Delphi I can use it this way:

function click_handler(sender):
begin
    checked_box := sender.tag;
end;

Here I remember current checked box number in some variable (tag property was manually set in Delphi IDE).

I searched and can't find how I can implement this using Qt, because event handlers don't have sender argument.

I implemented it like this, but it's not convenient:

# assign handlers (n assignments)
checkbox_1.clicked.connect(self.cb_1_click)
...
checkbox_<n>.clicked.connect(self.cb_<n>_click)

# separate handler for each check box (n functions, doing the same stupid work)
def cb_1_click:
    self.cb_click(sender=1)
...
def cb_<n>_click:
    self.cb_click(sender=n)

# main check box click logic (1 function)
def cb_click(sender):
    # do something common for all checkboxes

Thank you.

+3  A: 

You may use QObject::sender() in slots to find out who emitted the signal.
Also you might want to check out QSignalMapper which is intended exactly for solving these problems.

Idan K