开发者

PyQt, Qt, one event handler working with many items

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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜