GTK+ How do I find which radio button is selected?
Asked Answered
C

8

6

The tutorial here http://developer.gnome.org/gtk-tutorial/2.90/x542.html shows how to set up the radio buttons, but neglects to tell you how to use them.

How do I then find which radio button is selected?

My solution:

Initialise radio buttons with:

rbutton1 = gtk_radio_button_new_with_label(NULL, "button1");
gtk_box_pack_start(GTK_BOX(rbutton_box), rbutton1, TRUE, TRUE, 0);

rbuttonGroup = gtk_radio_button_get_group(GTK_RADIO_BUTTON(rbutton1)); /*not sure what I'd use this line for currently though*/
rbutton2 = gtk_radio_button_new_with_label_from_widget(GTK_RADIO_BUTTON(rbutton1), "button 2"); 
gtk_box_pack_start(GTK_BOX(rbutton_box), rbutton2, TRUE, TRUE, 0);

rbutton3 = gtk_radio_button_new_with_label_from_widget(GTK_RADIO_BUTTON(rbutton1), "button 3"); 
gtk_box_pack_start(GTK_BOX(rbutton_box), rbutton3, TRUE, TRUE, 0);

And update a variable telling you which radio button is selected with this method:

        void checkRadioButtons()
{
    if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON(rbutton1))==TRUE) selectedRadioButton =1;
    if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON(rbutton2))==TRUE) selectedRadioButton =2;
    if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON(rbutton3))==TRUE) selectedRadioButton =3;
}
Cudbear answered 11/1, 2012 at 0:4 Comment(0)
P
5

Google brought me here for python / pygtk / pygtk3 searches, so I hope its okay that I post a pygtk solution:

def _resolve_radio(self, master_radio):
    active = next((
        radio for radio in
        master_radio.get_group()
        if radio.get_active()
    ))
    return active

This uses a generator to return the first (which should be the only) active radio box that is active.

Psychrometer answered 11/1, 2012 at 0:4 Comment(0)
P
2

Here's how I suggest doing it:

void radio_button_selected (GtkWidget *widget, gpointer data) 
{
    if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (widget))
    {
        GSLIST *group = gtk_radio_button_get_group (GTK_RADIO_BUTTON (widget));
        g_print ("Index = %i%\n", g_slist_index (group, widget));
    }
}
Perutz answered 21/6, 2016 at 18:58 Comment(0)
D
1

Let's create a serie of buttons :

for severity in levels:
    radio = gtk.RadioButton(group=radioButtons, label=severity)
    if severity == actualLevel:
        radio.set_active(True)
    hBox.pack_start(radio, True, True, 3)
    radio.connect('toggled', self.radioButtonSelected, severity)

and all buttons are connected to the same handler :

def radioButtonSelected(self, button, currentSeverity):
    # proceed with the task
    # as you can see, button is passed by as argument by the event handler
    # and you can, for example, get the button label :
    labelReadFromButton = button.getLabel()
Delphina answered 11/1, 2012 at 13:0 Comment(0)
C
1

This is how I do it.

GtkRadioButton * radio_button;
GtkRadioButton * radio_button1;
GtkRadioButton * radio_button2;
...
GSList * tmp_list = gtk_radio_button_get_group (radio_button);//Get the group of them.
GtkToggleButton *tmp_button = NULL;//Create a temp toggle button.

while (tmp_list)//As long as we didn't reach the end of the group.
{
  tmp_button = tmp_list->data;//Get one of the buttons in the group.
  tmp_list = tmp_list->next;//Next time we're going to check this one.

  if (gtk_toggle_button_get_active(tmp_button))//Is this the one active?
    break;//Yes.

  tmp_button = NULL;//We've enumerated all of them, and none of them is active.
}
//Here. tmp_button holds the active one. NULL if none of them is active.

See the discussion here. I don't know if they will add this function into it (seems not).

Claytonclaytonia answered 25/4, 2014 at 7:56 Comment(0)
C
0

You may connect to the GtkToggleButton::toggled signal instead. In the associated callback, you'll be able to update your variable. As for the call to gtk_radio_button_get_group, you only need it if you call gtk_radio_button_new_with_label instead of gtk_radio_button_new_with_label_with_widget, as specified in the tutorial you're refering to.

Chide answered 11/1, 2012 at 10:57 Comment(0)
U
0

Use lambda expressions if you dont want to mess around with the annoying methods, still have to use connect though, but its alot easier to read:

Enum RadioValues { A, B, C, none };

RadioValues values = RadioValues.none; // only needed if you dont have an initially selected radio button

MyConstructor()
{
   Build();
   // asumming you have 3 radio buttons: radioA, radioB, radioC:
   radioA.Toggled += (sender,e) => values = RadioValues.A;
   radioB.Toggled += (sender,e) => values = RadioValues.B;
   radioC.Toggled += (sender,e) => values = RadioValues.C;

}

and thats it, no methods to deal with, and you dont have to restrict yourself to just that either, you can also use an anonymous function if you need more flex--of course the next step after that is using methods. Unfortunately they didnt offer a simple .Checked property, my next suggestion is to override the radio button itself and chain a Checked property when it's toggled state is changed, emulating other frameworks like MFC, Qt, and Winforms... etc.

PS: I left out boilerplate code for simplicity's sake, which can make answers a bit more muddled and you probably just want the facts not a demonstration on whether or not I can properly call a constructor :)

Uvea answered 17/11, 2013 at 3:31 Comment(0)
W
0

My solution for GTKmm is quite easier,

You just have to call the function :

my_radio_button.get_active(); \n

This will return either 0 if its unactive or 1 if its active.

Wasteful answered 27/10, 2019 at 15:22 Comment(0)
E
0

This is a demo code using Radio Buttons, where you can find how I find which radio button is selected:

#include <gtkmm/window.h>
#include <gtkmm/box.h>
#include <gtkmm/radiobutton.h>
#include <gtkmm/separator.h>
#include <gtkmm/application.h>
#include <iostream>

class ButtonWindow : public Gtk::Window
{
private:
   //Child widgets:
   Gtk::Box m_Box_Top, m_Box1, m_Box2;
   Gtk::RadioButton m_RadioButton1, m_RadioButton2, m_RadioButton3;
   Gtk::Separator m_Separator;
   Gtk::Button m_Button_Close;
   Gtk::RadioButton *m_SelectedButton{nullptr};

public:
   ButtonWindow()
      : m_Box_Top(Gtk::ORIENTATION_VERTICAL),
        m_Box1(Gtk::ORIENTATION_VERTICAL, 15),
        m_Box2(Gtk::ORIENTATION_VERTICAL, 0),
        m_RadioButton1("button 1"),
        m_RadioButton2("button 2"),
        m_RadioButton3("button 3"),
        m_Button_Close("close")
   {
      // Set title and border of the window
      set_title("radio buttons");
      set_border_width(0);

      // Put radio buttons 2 and 3 in the same group as 1:
      m_RadioButton2.join_group(m_RadioButton1);
      m_RadioButton3.join_group(m_RadioButton1);

      // Add outer box to the window (because the window
      // can only contain a single widget)
      add(m_Box_Top);

      //Put the inner boxes and the separator in the outer box:
      m_Box_Top.pack_start(m_Box1);
      m_Box_Top.pack_start(m_Separator);
      m_Box_Top.pack_start(m_Box2);

      // Set the inner boxes' borders
      m_Box1.set_border_width(20);
      m_Box2.set_border_width(10);

      // Put the radio buttons in Box1:
      m_Box1.pack_start(m_RadioButton1);
      m_Box1.pack_start(m_RadioButton2);
      m_Box1.pack_start(m_RadioButton3);

      // Put Close button in Box2:
      m_Box2.pack_start(m_Button_Close);

      // Connect the button signals:
#if 1 // Full C++11: (change this to #if 0 to use the traditional way)
      m_RadioButton1.signal_clicked().connect([&]{on_radio_button_clicked(m_RadioButton1);});
      m_RadioButton2.signal_clicked().connect([&]{on_radio_button_clicked(m_RadioButton2);});
      m_RadioButton3.signal_clicked().connect([&]{on_radio_button_clicked(m_RadioButton3);});

      m_Button_Close.signal_clicked().connect([&]{on_close_button_clicked();});
#else // Traditional:
  m_RadioButton1.signal_clicked() // Full sigc
     .connect(sigc::bind(sigc::mem_fun(*this, &ButtonWindow::on_radio_button_clicked),
                         sigc::ref(m_RadioButton1)));

  m_RadioButton2.signal_clicked() // sigc && C++98
     .connect(std::bind(sigc::mem_fun(*this, &ButtonWindow::on_radio_button_clicked),
                        std::ref(m_RadioButton2)));

  m_RadioButton3.signal_clicked() // Full C++98
     .connect(std::bind(&ButtonWindow::on_radio_button_clicked, this,
                        std::ref(m_RadioButton3))); 

      m_Button_Close.signal_clicked()
         .connect(sigc::mem_fun(*this, &ButtonWindow::on_close_button_clicked));
#endif

      // Set the second button active:
      m_RadioButton2.set_active();

      // Make the close button the default widget:
      m_Button_Close.set_can_default();
      m_Button_Close.grab_default();

      // Show all children of the window:
      show_all_children();
   }
  
protected:
   //Signal handlers:
   void on_radio_button_clicked(Gtk::RadioButton& button)
   {
      if(m_SelectedButton != &button && button.get_active())
      {
         m_SelectedButton = &button;
         std::cout << "Radio "<< m_SelectedButton->get_label() << " selected.\n";
      }
   }

   void on_close_button_clicked()
   {
      hide(); // Close the application
   }
};

int main(int argc, char *argv[])
{
   auto app = Gtk::Application::create(argc, argv, "org.gtkmm.example");

   ButtonWindow button;

   //Shows the window and returns when it is closed.
   return app->run(button);
}
Erse answered 13/8, 2020 at 23:4 Comment(3)
Could you clarify how this answers the question being asked?Colpitis
Yes sir, I added in the first line that the example shows how I find which radio button is selected. In replying to you, I realized now that I used gtkmm and not gtk+ as the question asks. Sorry, but I think this code can be useful anyway.Erse
I want to clarify also that I wrote different ways to connect the signals. The first is my way of doing it in C++11. You can choose it with #if 1, if you change the if to #if 0 you will have the form explained in the gtkmm manual. However, in that case, I also wrote three alternatives to write the signals.Erse

© 2022 - 2024 — McMap. All rights reserved.