Knopf

5.1.1. Konstruktoren

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);

Stock items have been recommended for use in buttons. From gtkmm-3.10 they are deprecated. They should not be used in newly-written code. However, the documentation of namespace Gtk::Stock shows recommended labels and named icons to show in buttons.

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

Reference

5.1.2. Beispiel

Dieses Beispiel erzeugt einen Knopf mit einer Grafik und einer Beschriftung.

Abbildung 5-1Beispiel für Knöpfe

Source Code

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: 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;
}

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

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

int main(int argc, char *argv[])
{
  Glib::RefPtr<Gtk::Application> app = Gtk::Application::create(argc, argv, "org.gtkmm.example");

  Buttons buttons;

  //Shows the window and returns when it is closed.
  return app->run(buttons);
}

5.1.3. Signale

Das Gtk::Button-Widget gibt folgende Signale aus, wobei Sie jedoch meist nur das clicked-Signal verarbeiten müssen:

pressed

Wird ausgegeben, wenn der Knopf gedrückt wird.

released

Wird ausgegeben, wenn der Knopf losgelassen wird.

clicked

Wird ausgegeben, wenn der Knopf angeklickt und losgelassen wird.

enter

Wird ausgegeben, wenn sich der Mauszeiger über dem Fenster mit dem Knopf befindet.

leave

Wird ausgegeben, wenn der Mauszeiger das Fenster mit dem Knopf verlässt.