Cet article fait parti d’un ensemble d’exemples et d’articles sur gtkmm consultables ici. Cet exemple montre comment créer un container (Gtk::Grid) pour y insérer des boutons dont un à cheval sur les deux autres:
Téléchargement

gtkmm - example 3 1.92 KB
Code source
main.cpp
#include
#include
int main(int argc, char* argv[])
{
// Initialize gtkmm
Glib::RefPtr app = Gtk::Application::create(argc, argv, "www.lucidarme.me");
// Create the window
mainwindow w;
// Start main loop
return app->run(w);
}
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include
#include
#include
// The class mainwindow inherits from Gtk::Window
class mainwindow : public Gtk::Window
{
// Constructor and destructor
public:
mainwindow();
virtual ~mainwindow();
protected:
//Signal handlers (run when the button are clicked)
void on_button_1();
void on_button_2();
//Member widgets:
Gtk::Button button1;
Gtk::Button button2;
Gtk::Button buttonQuit;
Gtk::Grid mainGrid;
};
#endif // MAINWINDOW_H
mainwindow.cpp
#include "mainwindow.h"
#include
// Constructor of the main Window (build ui interface).
mainwindow::mainwindow()
{
// Initialize the main window
this->set_title("Grid");
this->set_border_width(10);
// Create and connect the first button
button1.add_label("Button 1");
button1.signal_clicked().connect(sigc::mem_fun(*this,&mainwindow::on_button_1));
mainGrid.attach(button1,0,0,1,1);
// Create and connect the second button
button2.add_label("Button 2");
button2.signal_clicked().connect(sigc::mem_fun(*this,&mainwindow::on_button_2));
mainGrid.attach(button2,1,0,1,1);
// Create and connect the Quit button
buttonQuit.add_label("Quit");
buttonQuit.signal_clicked().connect(sigc::mem_fun(*this,&mainwindow::close));
mainGrid.attach(buttonQuit,0,1,2,1);
// Display the main grid in the main window
mainGrid.show_all();
add(mainGrid);
}
// Destructor of the class
mainwindow::~mainwindow()
{}
// Call when the first button is clicked
void mainwindow::on_button_1()
{
std::cout << "Button 1" << std::endl;
}
// Call when the second button is clicked
void mainwindow::on_button_2()
{
std::cout << "Button 2" << std::endl;
}