RadioButton

Like checkboxes, radio buttons also inherit from Gtk::ToggleButton, but these work in groups, and only one RadioButton in a group can be selected at any one time.

Groups

There are two ways to set up a group of radio buttons. The first way is to create the buttons, and set up their groups afterwards. Only the first two constructors are used. In the following example, we make a new window class called RadioButtons, and then put three radio buttons in it:

  class RadioButtons : public Gtk::Window
  {
  public:
      RadioButtons();

  protected:
      Gtk::RadioButton m_rb1, m_rb2, m_rb3;
  };

  RadioButtons::RadioButtons()
    : m_rb1("button1"),
      m_rb2("button2"),
      m_rb3("button3")
  {
      Gtk::RadioButton::Group group = m_rb1.get_group();
      m_rb2.set_group(group);
      m_rb3.set_group(group);
  }
We told gtkmm to put all three RadioButtons in the same group by obtaining the group with get_group() and using set_group() to tell the othe RadioButtons to share that group.

Note that you can't just do

m_rb2.set_group(m_rb1.get_group()) //doesn't work
because the group is modified by set_group() and therefore non-const.

The second way to set up radio buttons is to make a group first, and then add radio buttons to it. Here's an example:

  class RadioButtons : public Gtk::Window
  {
  public:
      RadioButtons();
  };

  RadioButtons::RadioButtons()
  {
      Gtk::RadioButton::Group group;
      Gtk::RadioButton *m_rb1 = manage( new Gtk::RadioButton(group,"button1"));
      Gtk::RadioButton *m_rb2 = manage( new Gtk::RadioButton(group,"button2"));
      Gtk::RadioButton *m_rb3 = manage( new Gtk::RadioButton(group,"button3"));
  }

We made a new group by simply declaring a variable, group, of type Gtk::RadioButton::Group. Then we made three radio buttons, using a constructor to make each of them part of group.

Methods

RadioButtons are "off" when created; this means that when you first make a group of them, they will all be off. Don't forget to turn one of them on using set_active():

Reference

Example

The following example demonstrates the use of RadioButtons:

Figure 4.3. RadioButton

Source Code