Table of Contents
gtkmm provides four basic types of buttons:
Gtk::Button
. Standard buttons, usually
marked with a label or picture. Pushing one triggers an action. See the Button section.
Gtk::ToggleButton
.
Unlike a normal Button, which springs back up, a ToggleButton stays down until you
press it again. It might be useful as an on/off switch. See the ToggleButton section.
Gtk::CheckButton
.
These act like ToggleButtons, but show their state in small squares,
with their label at the side. They should be used in most situations
which require an on/off setting.
See the CheckButton section.
Named after the station selectors on old car radios, these buttons are used in groups for options which are mutually exclusive. Pressing one causes all the others in its group to turn off. They are similar to ToggleButtons or CheckButtons (a small widget with a label at the side), but usually look different. There is no separate radio button class. Check buttons and toggle buttons can act as radio buttons. See the Radio Button section.
Note that, due to GTK's theming system, the appearance of these widgets will vary. In the case of check buttons and radio buttons, they may vary considerably.
There are two ways to create a Button. You can specify a label
string in the Gtk::Button
constructor,
or set it later with set_label()
.
To define an accelerator key for keyboard navigation, place an underscore before one of the label's characters and specify true
for the optional mnemonic
parameter. For instance:
Gtk::Button* pButton = new Gtk::Button("_Something", true);
Gtk::Button
is also
a container so you could put any other widget, such as a
Gtk::Image
into it.
The Gtk::Button
widget has the clicked
signal
which is emitted when the button is pressed and released.
This example creates a button with a picture and a label.
File: buttons.h
(For use with gtkmm 4)
#ifndef GTKMM_EXAMPLE_BUTTONS_H #define GTKMM_EXAMPLE_BUTTONS_H #include <gtkmm/window.h> #include <gtkmm/button.h> class Buttons : public Gtk::Window { public: Buttons(); virtual ~Buttons(); protected: //Signal handlers: void on_button_clicked(); //Child widgets: Gtk::Button m_button; }; #endif //GTKMM_EXAMPLE_BUTTONS_H
File: buttons.cc
(For use with gtkmm 4)
#include "buttons.h" #include <gtkmm/box.h> #include <gtkmm/image.h> #include <gtkmm/label.h> #include <iostream> Buttons::Buttons() { // This corresponds to Gtk::Bin::add_pixlabel("info.xpm", "cool button") in gtkmm3. //Create Image and Label widgets: auto pmap = Gtk::make_managed<Gtk::Image>("info.xpm"); auto label = Gtk::make_managed<Gtk::Label>("cool button"); label->set_expand(true); //Put them in a Box: auto hbox = Gtk::make_managed<Gtk::Box>(Gtk::Orientation::HORIZONTAL, 5); hbox->append(*pmap); hbox->append(*label); //And put that Box in the button: m_button.set_child(*hbox); set_title("Pixmap'd buttons!"); m_button.signal_clicked().connect( sigc::mem_fun(*this, &Buttons::on_button_clicked) ); m_button.set_margin(10); set_child(m_button); } Buttons::~Buttons() { } void Buttons::on_button_clicked() { std::cout << "The Button was clicked." << std::endl; }
File: main.cc
(For use with gtkmm 4)
#include "buttons.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<Buttons>(argc, argv); }