Radio Button
There is no separate class for radio buttons. Check buttons and toggle buttons act as radio buttons when they form a group. Only one button in a group can be selected at any one time.
6.4.1. Grupos
You create the buttons, and set up their group afterwards. In the following example, we put 3 radio buttons in a group:
auto rb1 = Gtk::make_managed<Gtk::CheckButton>("button1"); auto rb2 = Gtk::make_managed<Gtk::CheckButton>("button2"); auto rb3 = Gtk::make_managed<Gtk::CheckButton>("button3"); rb2->set_group(*rb1); rb3->set_group(*rb1);
We told gtkmm to put all three CheckButtons in the same group by using set_group() to tell the other CheckButtons to share group with the first CheckButton.
6.4.2. Métodos
CheckButtons and ToggleButtons 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().
6.4.3. Exemplo
The following example demonstrates the use of grouped CheckButtons:
File: radiobuttons.h (For use with gtkmm 4)
#ifndef GTKMM_EXAMPLE_RADIOBUTTONS_H #define GTKMM_EXAMPLE_RADIOBUTTONS_H #include <gtkmm/box.h> #include <gtkmm/window.h> #include <gtkmm/button.h> #include <gtkmm/checkbutton.h> #include <gtkmm/separator.h> class RadioButtons : public Gtk::Window { public: RadioButtons(); virtual ~RadioButtons(); protected: //Signal handlers: void on_button_clicked(); //Child widgets: Gtk::Box m_Box_Top, m_Box1, m_Box2; Gtk::CheckButton m_RadioButton1, m_RadioButton2, m_RadioButton3; Gtk::Separator m_Separator; Gtk::Button m_Button_Close; }; #endif //GTKMM_EXAMPLE_RADIOBUTTONS_H
File: main.cc (For use with gtkmm 4)
#include "radiobuttons.h" #include <gtkmm/application.h> int main(int argc, char *argv[]) { auto app = Gtk::Application::create("org.gtkmm.example"); //Shows the window and returns when it is closed. return app->make_window_and_run<RadioButtons>(argc, argv); }
File: radiobuttons.cc (For use with gtkmm 4)
#include "radiobuttons.h" RadioButtons::RadioButtons() : m_Box_Top(Gtk::Orientation::VERTICAL), m_Box1(Gtk::Orientation::VERTICAL, 10), m_Box2(Gtk::Orientation::VERTICAL, 10), m_RadioButton1("button1"), m_RadioButton2("button2"), m_RadioButton3("button3"), m_Button_Close("close") { // Set title and border of the window set_title("radio buttons"); // Gtk::CheckButton and Gtk::ToggleButton have set_group() methods. // They act as radio buttons, if they are included in a group. // Put radio buttons 2 and 3 in the same group as 1: m_RadioButton2.set_group(m_RadioButton1); m_RadioButton3.set_group(m_RadioButton1); // Add outer box to the window (because the window // can only contain a single widget) set_child(m_Box_Top); //Put the inner boxes and the separator in the outer box: m_Box_Top.append(m_Box1); m_Box_Top.append(m_Separator); m_Box_Top.append(m_Box2); m_Separator.set_expand(); // Set the inner boxes' margins m_Box1.set_margin(10); m_Box2.set_margin(10); // Put the radio buttons in Box1: m_Box1.append(m_RadioButton1); m_Box1.append(m_RadioButton2); m_Box1.append(m_RadioButton3); m_RadioButton1.set_expand(); m_RadioButton2.set_expand(); m_RadioButton3.set_expand(); // Set the second button active m_RadioButton2.set_active(true); // Put Close button in Box2: m_Box2.append(m_Button_Close); m_Button_Close.set_expand(); // Make the button the default widget set_default_widget(m_Button_Close); // Connect the toggled signal of the button to // RadioButtons::on_button_toggled() m_Button_Close.signal_clicked().connect(sigc::mem_fun(*this, &RadioButtons::on_button_clicked) ); } RadioButtons::~RadioButtons() { } void RadioButtons::on_button_clicked() { hide(); //to close the application. }