Working through the gtkmm tutorial (Part 4)

Ok… The elections have been over for a few days but its been kind of hard to get back into my morning groove of study before work… Anyway lets see what we can do here. I’ve been sort of stuck the last couple of days around Hello World in gtkmm in the tutorial. There’s alot of good content in this tutorial, but someone I’m a different wavelength so far so I’m having a hard time staying focused on it. This sort of got me sort of unfocused browsing mode in all things gtkmm in my pc at the moment. For example… I installed gtkmm on my Ubuntu Linux box and there very interesting image of the gtkmm object hierarchy located locally on you machine… at /usr/share/doc/gtkmm-2.4/docs/reference/widget_hierarchy.png  .  I wanted to post a link but I could really find this quickly with google images.

I noticed there was a html file showing the widget hierarchy.  I thought I could just past that into this post, put that isn’t going to work… But… I just discovered something very useful.   The whole thing is interactive.  Click on the object and you go to the documentation.  I see at the bottom this was generated by Doxygen…

Actually what got me curious in the main.cc of the hello world tutorial

#include <gtkmm/main.h> #include “helloworld.h” int main (int argc, char *argv[]) { Gtk::Main kit(argc, argv); HelloWorld helloworld; //Shows the window and returns when it is closed. Gtk::Main::run(helloworld); return 0; }

The tutorial states that any time you use gtkmm you need this in your main function call: Gtk::Main kit(argc, argv); and it follows that if you have that you need this also #include <gtkmm/main.h> . I was looking at the “/usr/include/gtkmm-2.4/gtkmm/main.h”.  There is so much going on there, I’m not going to get myself off track there.  Very interesting stuff, but I think I found just doing to  add that  to my  to do list.. Studying how the gtkmm wrapper is designed to go around gtkmm.  But as they say that’s for another day..  Hmm… That’s an idea… adding  a todo list to my blog… Ehh… So this project has the following files the project

  • helloworld.cc
  • helloworld.h
  • main.cc

Looking through main.cc nothing really confuses… I suppose lets look at the header file helloworld.h

#ifndef GTKMM_EXAMPLE_HELLOWORLD_H
#define GTKMM_EXAMPLE_HELLOWORLD_H

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

class HelloWorld : public Gtk::Window
{

public:
HelloWorld();
virtual ~HelloWorld();

protected:
//Signal handlers:
virtual void on_button_clicked();

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

#endif // GTKMM_EXAMPLE_HELLOWORLD_H

Ok.. Lets see… I understand whats going on with

class HelloWorld : public Gtk::Window

But its not burned into memory on how to describe it..  Let me check… Hmm.  The site I had in mind isn’t book marked in my favorites yet…. I think I have hard copy somewhere buried on my desk………… Not under the cat…. Ok… Here it is … Published by Juan Soulie…. No hyperlink on the printout… Lil google and here’s the link: http://www.cplusplus.com/doc/tutorial/ I haven’t looked at this since I first started this endeavor.  As I recall, it was so concise you needed it was giving me brain overload…. Well I sort of understand this stuff again and lets give it a try. Bother time for work..  Ok…. I’m back from work and nice and quite in the morning.
Yikes, I just re-read my post.  It required a bunch of editing…  Oopps.
Anyway… Working from the inside out.
The “::” is known as the scoping operator. Quote from the tutorial:classes(I)

The scope operator (::) specifies the class to which the member being declared belongs, granting exactly the same scope properties as if this function definition was directly included within the class definition.

That seems straight forward enough..
Refering to tutorial(inheritance) Now the “:” is the mechanism by which the derived class “Helloworld” inherits all the public member functions of the base class.
So as far as the “Class”, at this point I get it…

At this point, I feel like I have a pretty good grasp has far as whats going on with the constructor

HelloWorld();

and the destructor sort of makes sense to me, but I’m still a little fuzzy on the term virtual….

virtual ~HelloWorld();

The going through the tutorial reference of polymorphism to understand this.
I ran through the tutorial and I understand what virtual does but not well enough to explain it. So… I’m going to quote from the tutorial:

Therefore, what the virtual keyword does is to allow a member of a derived class with the same name as one in the base class to be appropriately called from a pointer, and more precisely when the type of the pointer is a pointer to the base class but is pointing to an object of the derived class, as in the above example.

A class that declares or inherits a virtual function is called a polymorphic class.

I see this virtual constantly, with destructors in source code I’ve been perusing I did a quick google search and found some a nice explanation on why it’s done.
http://www.ehow.com/how_2156256_use-c-virtual-destructors.html
I guess the purpose to this prevent memory leaks.

On the rest of stuff in the helloworld.h seems fairly clear to me, but I’m sure I’ll have some issues when I dig into actual function definitions in helloworld.cc. Well lets see:

#include “helloworld.h”
#include

HelloWorld::HelloWorld()
: m_button(“Hello World”) // creates a new button with label “Hello World”.
{
// Sets the border width of the window.
set_border_width(10);

// When the button receives the “clicked” signal, it will call the
// on_button_clicked() method defined below.
m_button.signal_clicked().connect(sigc::mem_fun(*this,
&HelloWorld::on_button_clicked));

// This packs the button into the Window (a container).
add(m_button);

// The final step is to display this newly created widget…
m_button.show();
}

HelloWorld::~HelloWorld()
{
}

void HelloWorld::on_button_clicked()
{
std::cout << “Hello World” << std::endl;
}

I was just thinking I don’t even think I got this thing to run yet..
Lets see….

jonas@Ubuntu4:~/gtkmm_examples/book/helloworld$ ls
helloworld.cc helloworld.h main.cc
jonas@Ubuntu4:~/gtkmm_examples/book/helloworld$ g++ main.cc -o helloworld pkg-config gtkmm-2.4 --cflags --libs
/tmp/ccwMhi0L.o: In function main':
main.cc:(.text+0x143): undefined reference to
HelloWorld::HelloWorld()’
main.cc:(.text+0x160): undefined reference to HelloWorld::~HelloWorld()'
main.cc:(.text+0x173): undefined reference to
HelloWorld::~HelloWorld()’
collect2: ld returned 1 exit status
jonas@Ubuntu4:~/gtkmm_examples/book/helloworld$

Now that is strange….
Doh….. I found my this exact error on a Polish ubuntu site….. I wish I could read Polish (it looks like they gave the guy who made the post a little grief, but it gave me the clue to the now obvious mistake I made. Me bad… Let me try this…

jonas@Ubuntu4:~/gtkmm_examples/book/helloworld$ ls
helloworld.cc helloworld.h main.cc
jonas@Ubuntu4:~/gtkmm_examples/book/helloworld$ g++ main.cc helloworld.cc -o helloworld pkg-config gtkmm-2.4 --cflags --libs
jonas@Ubuntu4:~/gtkmm_examples/book/helloworld$ ls
helloworld helloworld.cc helloworld.h main.cc
jonas@Ubuntu4:~/gtkmm_examples/book/helloworld$ ./helloworld
Hello World

Much Much better 🙂
Ok… So much for the quiet Saturday morning…. The whole family from the Wife down to the dogs are coming in to say good morning…..
Ok… The wife is surfing the daughter is working on a computer game… so onward to helloworld.cc
Soo the code section looks like:

#include “helloworld.h”
#include <iostream>

HelloWorld::HelloWorld()
: m_button(“Hello World”)   // creates a new button with label “Hello World”.
{
// Sets the border width of the window.
set_border_width(10);

// When the button receives the “clicked” signal, it will call the
// on_button_clicked() method defined below.
m_button.signal_clicked().connect(sigc::mem_fun(*this,
&HelloWorld::on_button_clicked));

// This packs the button into the Window (a container).
add(m_button);

// The final step is to display this newly created widget…
m_button.show();
}

HelloWorld::~HelloWorld()
{
}

void HelloWorld::on_button_clicked()
{
std::cout << “Hello World” << std::endl;
}

The first thing I need to refresh on is the use of the initialiser [ or is an initializer] statement.

HelloWorld::HelloWorld()
: m_button(“Hello World”) // creates a new button with label “Hello World”.
{

I vaguely remember running across this in the Kuntz course. Lets see if I can find a better explanation..
Hmm. I was googling to see if what I could find and I just ran past an interesting post on the ubuntu forum:looking for a _sane_ way to use GTK with C++

One of the links points to: Gtk::builder
And yet another tutorial on how to use it:
GTK+ and Glade3 GUI Programming Tutorial – Part 1
This also led me to: VB Programmer’s Intro to Linux Programming with GTK+
Ohhh boy this guy has a lot of cool stuff how about this:
Using Your PC Sound Card As an Oscilloscope in Linux

Ok…. This has got me very intrigued. Working through this gtkmm hasn’t been exactly what I call results orientated.
This direct gtkmm coding approach is somewhat tedious…. I think I’m going to give this glade a look.

This entry was posted in OpenCascade, Ubuntu, Uncategorized. Bookmark the permalink.

Leave a Reply

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