Single handler for 9 buttons (gtkmm-2, libsigc++)

630 views Asked by At

I'm trying to make a widget with 9 toggle-buttons (3 rows and 3 columns), the user shall be able to select one of those. So i wrote 9 event handlers, which disabled the currently selected button and store the selection.

I just wanted to know if there is any way to connect those 9 toggle buttons to one single handler, and if there is a way to know which button emitted the signal. Something similar to QT's

QObject::sender()

This would avoid a lot of repetition in my code. As noted in the title I am using gtkmm version 2 and of course sigc++.

Thank you in advice

2

There are 2 answers

1
ergosys On BEST ANSWER

I've used c++11 lambdas to solve a similar problem. Basically you capture the information about the target in the lambda so when the signal fires you can do something target specific. For example:

abutton.signal_clicked().connect([this,&abutton]{ handle_click(abutton); });

I believe this will work with gtkmm 2.x, but I have only used it with 3.0 and I don't know how sigc++ has changed between the two versions. If you can't use c++11 features, you might be able to do something similar with the lambda functionality that comes with sigc++, but I've no experience with it.

1
ptomato On

I'm not that familiar with gtkmm, but in plain C GTK, the signal handler looks like this:

void on_button_toggled (GtkToggleButton *button, gpointer user_data);

The button pointer is the 'sender' in QT terminology. To find out which button is which, you could name them, or attach data with g_object_set_data().