1.1. Hello World!

Starting with a “Hello World!” example has been the traditional of most programming tutorials, and so is this one. Let’s begin our gtkmm tutorial by creating a 200 x 200 window with title “Hello World” (Source Code: HelloWorld.cc):


#include <gtkmm.h>

class HelloWorld : public Gtk::Window {
  virtual ~HelloWorld();

HelloWorld::HelloWorld() {
  set_title("Hello World");
  set_size_request(200, 200);

HelloWorld::~HelloWorld() {

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

  return 0;

Look scary, huh? Thought this is a traditional Hello World? This was what I felt four months ago. Well yeah it is a HelloWorld but it is just very “C++-ish”. Let’s examine the code together to understand it.

1. Header:
The file <gtkmm.h> includes all the definitions, functions, classes, etc… of gtkmm and other libraries that are used by gtkmm source code, such as glibmm. Although sometimes you need further files inclusions most of the time #include <gtkmm.h> is enough.

2. Define a derived class:
After including all the necessary headers, the first thing you need to do is to define a class that will create a Gtk::Window. Well you can absolutely just create a Gtk::Window in main() by using Gtk::Window helloworld; , and then set the title and size request for the helloworld later. However, since you are using gtkmm, you should try to write codes that are as organized and as “object-oriented” as you can. This will help you a lot later in when you have to create a complex gtkmm GUI, with lots of child widgets, signals, etc… (which we will see later)

class HelloWorld : public Gtk::Window {
  virtual ~HelloWorld();

The above code creates a HelloWorld class that is derived from the Gtk::Window class. It has a constructor, HelloWorld::HelloWorld() and a destructor HelloWorld::~HelloWorld(). We use this HelloWorld class later on to create a helloworld window by calling:

HelloWorld helloworld;

3. Constructor & Destructor:
Now let’s take a look at the constructor HelloWorld::HelloWorld():

HelloWorld::HelloWorld() {
  set_title("Hello World");
  set_size_request(200, 200);

Since the HelloWorld class is derived from the Gtk::Window base class, we can use all the public member functions of the Gtk::Window class directly inside class HelloWorld. We first need to set the title by calling the Gtk::Window::set_title() method of the Gtk::Window base class, which takes a Glib::ustring as its only argument (for now you can just pretend that a Glib::ustring is merely equal to std::string):

//set the title of the Gtk::Window
void Gtk::Window::set_title(const Glib::ustring& title);

/* title: title of the window 

The second function used inside the HelloWorld::HelloWorld() constructor (the set_size_request(200, 200) function) sets the request size for the newly created window. Unlike the Gtk::Window::set_title() function, Gtk::Widget::set_size_request() is a public member function of Gtk::Widget class, which is the base class of Gtk::Window (see the gtkmm reference for widget hierarchy). Since HelloWorld is derived from Gtk::Window, and Gtk::Window is derived from Gtk::Widget, we can use Gtk::Widget::set_size_request() inside our HelloWorld class:

//set the request size of the Gtk::Widget
void Gtk::Widget::set_size_request(int width = -1, 
                                   int height = -1);

/* width: the width of the widget in pixels
 * height: the height of the window in pixels. 

4. Initialize gtkmm:
After declaring the HelloWorld class, in order for your window to be displayed, you have to first initialize gtkmm by calling:

Gtk::Main kit(argc, argv);

inside the main() function. The Gtk::Main kit(argc, argv) line helps setting up the gtkmm and GTK+ environment. To create the a HelloWorld window, you can just simply create a HelloWorld object:

HelloWorld helloworld;

After creating helloworld, the HelloWorld::HelloWorld() constructor will automatically set the title and the request size of the helloworld window for us. This is what I really like about gtkmm: Everything is nested inside the HelloWorld class and I do not need to use dozens of functions inside my main() to set properties or pack widgets into my main window.

Finally, in order for your window to be displayed (drawed) on the screen, you need to trigger the main gtkmm loop. By calling:


the computer will start your program and draw the helloworld object on the screen.

5. Compile the source code:
Compiling the above source code by calling:

g++ -g /home/phongcao/helloworld.cc -o /home/phongcao/helloworld `pkg-config --cflags --libs gtkmm-2.4`

gives us the desired “helloworld” program. The result has already been shown in the above screenshot.


Tags: , , , , ,

7 Responses to “1.1. Hello World!”

  1. formwach Says:

    will this entry sowhere in the www &blogwide be continued ?
    this is one of the tutorials about the gtkmm that i count to the better once …
    please ?!?

  2. Phong Cao Says:

    Hi formwach,

    I am currently busy with developing my music manager software using Gtkmm & GStreamermm. I plan on releasing it this October and then continue to finish this tutorial by December. I will try to get the approval of Gtkmm team to put my tutorial on their website. Thank you for your appreciation! Please submit to my blog and come back here again by the start of December!

  3. formwach Says:

    that sounds great, when u need someone (else) to test our release &in an other country like germany, or maybe a translater from buttons oder releasenotes
    I would be glad to help…

    I teach myself C/Cpp about a year long, just for the interest in the computer science
    it’s great / sometimes scary to have ability to write the following context…

    the first little window need not the hole info from include gtkmm
    just gtkmm/window.h and gtkmm/main.h. ->will smart u’r .exe ./out -size

    An other solution, usefull course u need to handle w/ smaller code,
    of the member initialization syntax is..

    using namespace Gtk;

    class A : public Window {
    Label lab;

    : Window( ),
    lab(“Hello World !”)

    set_title(“Myfirst Window”);
    set_size_request(200, 200);


    }; /*end ctor A*/

    ~A() { };

    Main kit(argc,argv); A rrr; kit.run(rrr);

    • Phong Cao Says:

      Hi formwach,

      Thank you for the reply. Yeah I read all the main tutorial of Gtkmm and was aware of the #include <gtkmm.h> header while writing this chapter. And my codes might not be as succinct and as small as it needs to be.

      However, I did my best to write a tutorial that conforms to, or has its codes look much like the ones in the main gtkmm tutorial but with more detailed explanation so that newbies will be able to grasp the concept. I think the reasons why so many people do not understand gtkmm (including me about more than a year ago or so) is because when I started out I did not know what a Gtk::HBox or Gtk::VBox was, or what were the difference & relationship between a Gtk::Adjustment & a Gtk::HScale?. Everything in the main gtkmm tutorial is just a whole bunch of codes put together with supershort explanation that can no way capture the concepts behind creating & designing UI with gtkmm.

      Therefore, I really tried to write this tutorial to aim at explaining the basic OOP concepts of gtkmm programming. I wrote the examples so that they would look much similar to the ones in gtkmm main tutorial and thus the readers can use both of the documents interchangeably to learn gtkmm.

      Thank you for the comment about #include <gtkmm.h>. I will address this issue later in the later chapter when talking about linking and compiling gtkmm along with the main software package using Autotools…

  4. formwach Says:

    some other questions :
    why the destructor have a “pure” virtual method (?) / this info hold the gtkmm tutorial, but it’s not in my opinion to use it. i’m a novice, and just play around, where and how help this statment ? (put it just for the obligation to provide common C++ish-Style in my code ?) and why the newbie need -g /swich in his g++. Where is the point of meaningful relation of time to code my project / or rough draft from glade ? There’s a need to worry about glade, current plan my calculator ?

    • Phong Cao Says:

      Hi formwach,

      I have just written a new post explaining why we need a “pure” virtual destructor. Here is the post.

      Your second question: Why does newbie need -g in his g++. Well because they are newbie to gtkmm, but not Unix or C++. Here I assume that the newbie already knows what C++, GDB & Linux are, because if they do not know those stuffs then this whole tutorial will make no sense to them, no matter how easy it is to understand, right? You can not program C++/Gtkmm if you know nothing about Linux & GDB debugging.

      The last one, about Glade: I do not use Glade. The reason is that although Glade can help you rough-draft & even create your gtkmm interface, it lacks the customization ability that hard-coded gtkmm codes have.

      For example, I am developing a music manager called Dingo. I need to create a custom rating widget containing 5 stars that allows the users to rate the songs. I then need to integrate it into the Gtk::MenuItem widget. How am I gonna do that in Glade? Absolutely no way.

      Therefore, despite the comfortability & easiness of designing Gtkmm interface in Glade, I still preferred hard-coded ones. It is easier for me to extend the widgets of gtkmm to suit my needs.

      And look around you: Have you ever seen a large gtkmm software, like InkScape, GIMP, Ubuntu Software Center, etc… developed in Glade? I am not trying to lower the Glade project but I just want to share my thought!

  5. 1.2. Why do we need “pure” virtual destructor in HelloWorld? « Phong Cao Says:

    […] does the destructor of the HelloWorld example in this gtkmm tutorial need to be a “pure” virtual method? (put it just for the […]

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: