Botão
Existem duas formas de criar um Botão. Você pode especificar um texto de rótulo no construtor Gtk::Button ou defini-lo mais tarde com set_label().
Para definir uma tecla aceleradora para navegação por teclado, coloque uma sublinha antes de um dos caracteres do rótulo e especifique true para o parâmetro opcional mnemonic. Por exemplo:
Gtk::Button* pButton = new Gtk::Button("_Algo", true);
Gtk::Button é também um contêiner, então você pode colocar qualquer outro widget nele, como um Gtk::Image.
The Gtk::Button widget has the clicked signal which is emitted when the button is pressed and released.
- 6.1.1. Exemplo
6.1.1. Exemplo
Este exemplo cria um botão com uma figura e um rótulo.
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: 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); }
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; }