gtkmm – example 1

This is part of a more general work dedicated to gtkmm accessible here. This example is a classic first hello world program:

capture

Download

Source code

main.cpp

#include <mainwindow.h>
#include <gtkmm.h> 

int main(int argc, char* argv[])
{
    // Initialize gtkmm
    Gtk::Main app(argc, argv);
    // Create the window
    mainwindow w;
    // Start main loop
    Gtk::Main::run(w);
    return 0;
}

mainwindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <gtkmm/button.h>
#include <gtkmm/window.h>
#include <iostream>


// The class mainwindow inherits from Gtk::Window
class mainwindow : public Gtk::Window
{
	// Constructor and desturctor
public:    
	mainwindow();
	virtual             ~mainwindow();

protected:
	//Signal handlers (run when the button os clicked)
	void                  on_button_clicked();

	//Member widgets:
	Gtk::Button           button;
};


#endif // MAINWINDOW_H

mainwindow.cpp

#include "mainwindow.h"

// Constructor of the main Window (build ui interface).
mainwindow::mainwindow()
{
	// Resize the main window and set border width
	this->resize(200,50);
	set_border_width(10);

	// Add the label inside the button
	button.add_label("Hello world");
	// When the button receives the "clicked" signal, it calls the on_button_clicked() method
	button.signal_clicked().connect(sigc::mem_fun(*this,&mainwindow::on_button_clicked));

	// This packs the button into the Window (a container).
	add(button);
	// The final step is to display this newly created widget.
	button.show();
}


// Destructor of the class
mainwindow::~mainwindow()
{}

// Call when the button os clicked and display Hello world in the terminal
void mainwindow::on_button_clicked()
{
	std::cout << "Hello World" << std::endl;
}

One thought on “gtkmm – example 1

Leave a Reply

Your email address will not be published. Required fields are marked *