Botón

5.1.1. Constructores

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("_Algo", true);

Wherever possible you should use Stock items, to ensure consistency with other applications, and to improve the appearance of your applications by using icons. For instance,

Gtk::Button* pButton = new Gtk::Button(Gtk::Stock::OK);
This will use standard text, in all languages, with standard keyboard accelerators, with a standard icon.

Gtk::Button is also a container so you could put any other widget, such as a Gtk::Image into it.

Referencia

5.1.2. Ejemplo

Este ejemplo crea un botón con una imagen y una etiqueta.

Figura 5-1botones de ejemplo

Código fuente

File: buttons.h (For use with gtkmm 3, not gtkmm 2)

#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 3, not gtkmm 2)

#include "buttons.h"
#include <gtkmm/main.h>

int main(int argc, char *argv[])
{
  Gtk::Main kit(argc, argv);

  Buttons buttons;
  //Shows the window and returns when it is closed.
  Gtk::Main::run(buttons);

  return 0;
}

File: buttons.cc (For use with gtkmm 3, not gtkmm 2)

#include "buttons.h"
#include <iostream>

Buttons::Buttons()
{
  m_button.add_pixlabel("info.xpm", "cool button");

  set_title("Pixmap'd buttons!");
  set_border_width(10);

  m_button.signal_clicked().connect( sigc::mem_fun(*this,
              &Buttons::on_button_clicked) );

  add(m_button);

  show_all_children();
}

Buttons::~Buttons()
{
}

void Buttons::on_button_clicked()
{
  std::cout << "The Button was clicked." << std::endl;
}

5.1.3. Señales

The Gtk::Button widget has the following signals, but most of the time you will just handle the clicked signal:

pressed

Emitida cuando se pulsa el botón.

released

Emitida cuando se suelta el botón.

clicked

Emitida cuando el botón se pulsa y se suelta.

enter

Emitida cuando se mueve el puntero del ratón sobre el botón de la ventana.

leave

Emitida cuando el puntero del ratón sale del botón de la ventana.