RadioButton (单选按钮)
和 Gtk::CheckButton 一样,Gtk::RadioButton 也继承自 Gtk::ToggleButton,只是它们是按组工作的,并且一个组内一次只能选中一个 RadioButton。
5.4.1. 组
有两种方式创建一个单选按钮的组。第一种方式是先创建按钮,然后再设置它们的组。这种方式只用到了前两个构造函数。在下面的示例中,我们创建了一个新的窗口类 RadioButtons,然后在其中放置3个单选按钮:
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); }
我们告诉 gtkmm 把三个 RadioButton 都放到一个组内,通过 get_group() 来得到这个组,然后使用 set_group() 告诉其它的 RadioButton 来共享这个组。
注意,你不能只是
m_rb2.set_group(m_rb1.get_group()); //无法工作
第二种创建单选按钮的方式是,先创建一个组,然后再向该组加入按钮。请看下面的示例:
class RadioButtons : public Gtk::Window { public: RadioButtons(); }; RadioButtons::RadioButtons() { Gtk::RadioButton::Group group; Gtk::RadioButton *m_rb1 = Gtk::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")); }
我们通过声明一个类型为 Gtk::RadioButton::Group 的名为 group 的变量。然后我们创建三个单选按钮,用构造函数把它们放置到组 group 中。
5.4.3. 示例
接下来的例子中演示了 RadioButton 的用法:
File: radiobuttons.h (For use with gtkmm 3, not gtkmm 2)
#ifndef GTKMM_EXAMPLE_RADIOBUTTONS_H #define GTKMM_EXAMPLE_RADIOBUTTONS_H #include <gtkmm/box.h> #include <gtkmm/window.h> #include <gtkmm/radiobutton.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::RadioButton m_RadioButton1, m_RadioButton2, m_RadioButton3; Gtk::Separator m_Separator; Gtk::Button m_Button_Close; }; #endif //GTKMM_EXAMPLE_RADIOBUTTONS_H
File: radiobuttons.cc (For use with gtkmm 3, not gtkmm 2)
#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"); set_border_width(0); // Put radio buttons 2 and 3 in the same group as 1: Gtk::RadioButton::Group group = m_RadioButton1.get_group(); m_RadioButton2.set_group(group); m_RadioButton3.set_group(group); // 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_Box2.set_border_width(10); m_Box1.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); // Set the second button active m_RadioButton2.set_active(); // Put Close button in Box2: m_Box2.pack_start(m_Button_Close); // Make the button the default widget m_Button_Close.set_can_default(); m_Button_Close.grab_default(); // Connect the clicked signal of the button to // RadioButtons::on_button_clicked() m_Button_Close.signal_clicked().connect(sigc::mem_fun(*this, &RadioButtons::on_button_clicked) ); // Show all children of the window show_all_children(); } RadioButtons::~RadioButtons() { } void RadioButtons::on_button_clicked() { hide(); //to close the application. }
File: main.cc (For use with gtkmm 3, not gtkmm 2)
#include "radiobuttons.h" #include <gtkmm/application.h> int main(int argc, char *argv[]) { Glib::RefPtr<Gtk::Application> app = Gtk::Application::create(argc, argv, "org.gtkmm.example"); RadioButtons buttons; //Shows the window and returns when it is closed. return app->run(buttons); }