GTK+ Forums

Discussion forum for GTK+ and Programming. Ask questions, troubleshoot problems, view and post example code, or express your opinions.
It is currently Sun Nov 23, 2014 9:56 am

All times are UTC




Post new topic Reply to topic  [ 9 posts ] 
Author Message
 Post subject: C++, gtkmm and Glade
PostPosted: Wed Nov 30, 2011 10:32 pm 
Offline
Familiar Face

Joined: Tue Mar 16, 2010 9:35 pm
Posts: 24
Is anyone using this combo? And with C++ I mean OOP, not just C on steroids :-)

I've been writing in C and using Glade and GtkBuilder. Which is ok. However, I've decided to move to C++ since it solves some of the hassles with plain C (imo). But I'm not really sure how to design the gui programming code. It seems kind of a half-solution to write the GUI code using C++ if all the GTK code is kept in a main function, as in C.

Is anyone doing this? If so, how are you solving it and what tools do you use? (Tools, as in libglade, gtkmm etc.)


Top
 Profile  
 
 Post subject: Re: C++, gtkmm and Glade
PostPosted: Thu Dec 01, 2011 7:25 am 
Offline
Never Seen the Sunlight

Joined: Mon Apr 28, 2008 5:52 am
Posts: 777
Location: UK
Hi,

I use C++ Glade and Gtkmm. It is a combination that I like. One thing that you can not use is the C GtkBuilder signal auto connect system. So you have to write all of your signal connects in your code. Also I now do not use libglade or libglademm any more as these are now deprecated in preference to GtkBuilder.

One thing to note about Gtkmm is that not every thing has a C++ equivalent to the C functions. So sometimes you may need to use a C function, but this is rare.

_________________
E.


Top
 Profile  
 
 Post subject: Re: C++, gtkmm and Glade
PostPosted: Thu Dec 01, 2011 10:41 am 
Offline
Familiar Face

Joined: Tue Mar 16, 2010 9:35 pm
Posts: 24
Hi, thanks for your answer.

Could you give me a couple of pointers? In this simple test example I'm using libglade - how can I change this code to use GtkBuilder?

File elevator.cpp:
Code:
#include <gtkmm.h>
#include <libglademm/xml.h>
#include "Egui.h"

using namespace std;

int main (int argc, char *argv[]) {
     Glib::RefPtr<Gnome::Glade::Xml>elevator;
     Gtk::Main kit(argc, argv);
   
     // load the interface
     elevator = Gnome::Glade::Xml::create("src/Egui.glade");
   
     // Get the main window:
     Egui *mwindow;
     elevator->get_widget_derived("mwindow", mwindow);

     // start the event loop
     Gtk::Main::run(*mwindow );
   
     return 0;
}

File Egui.cpp:
Code:
#include "Egui.h"
#include <iostream>

using namespace std;

Egui::Egui (BaseObjectType* base_object, const Glib::RefPtr<Gnome::Glade::Xml>& glade_xml):Gtk::Window(base_object) {

     glade_xml->get_widget("mwindow", mwindow);
     glade_xml->get_widget("f5bttn", f5bttn);

   // Connect the appropriate callbacks...
     f5bttn->signal_clicked().connect(sigc::mem_fun(*this, &Egui::on_f5bttn_clicked));   // signal_clicked -> in the glade file: clicked

}

Egui::~Egui () {}

// Signal handler methods:
void Egui::on_f5bttn_clicked () {
   cout << "Mmmmm.....5......" <<endl;
}

File Egui.h:
Code:
class Egui:public Gtk::Window {
   public:
     Egui (BaseObjectType* cobject, const Glib::RefPtr<Gnome::Glade::Xml>& glade_xml);
     virtual ~Egui ();

   protected:
   Gtk::Window *mwindow;
   Gtk::Button *f5bttn;   

   void on_f5bttn_clicked ();
};


I'm using gcc/g++, and compiling/linking like this:
$ g++ -c src/Egui.cpp -export-dynamic `pkg-config gtkmm-2.4 libglademm-2.4 --cflags --libs`
g++ -c src/elevator.cpp -export-dynamic `pkg-config gtkmm-2.4 libglademm-2.4 --cflags --libs`
g++ -o testrun elevator.o Egui.o -export-dynamic `pkg-config gtkmm-2.4 libglademm-2.4 --cflags --libs`


Top
 Profile  
 
 Post subject: Re: C++, gtkmm and Glade
PostPosted: Thu Dec 01, 2011 10:58 am 
Offline
Familiar Face

Joined: Tue Nov 29, 2011 6:19 pm
Posts: 9
I'm quite noob at GUI's but i did something similar yesterday. I think changing all the references of Gnome::Glade::Xml to Gtk::Builder

try something like this:

File elevator.cpp:
Code:
#include <gtkmm.h>
#include <libglademm/xml.h>
#include "Egui.h"

using namespace std;

int main (int argc, char *argv[]) {
     Glib::RefPtr<Gtk::Builder> elevator;
     Gtk::Main kit(argc, argv);
   
     // load the interface
     elevator = Gtk::Builder::create("src/Egui.glade");
   
     // Get the main window:
     Egui *mwindow;
     elevator->get_widget_derived("mwindow", mwindow);

     // start the event loop
     Gtk::Main::run(*mwindow );
   
     return 0;
}



File Egui.cpp:
Code:
#include "Egui.h"
#include <iostream>

using namespace std;

Egui::Egui (BaseObjectType* base_object, const Glib::RefPtr<Gtk::Builder>& m_refBuiler):Gtk::Window(base_object) {

     m_refBuiler->get_widget("mwindow", mwindow);
     m_refBuiler->get_widget("f5bttn", f5bttn);

   // Connect the appropriate callbacks...
     f5bttn->signal_clicked().connect(sigc::mem_fun(*this, &Egui::on_f5bttn_clicked));   // signal_clicked -> in the glade file: clicked

}

Egui::~Egui () {}

// Signal handler methods:
void Egui::on_f5bttn_clicked () {
   cout << "Mmmmm.....5......" <<endl;
}


File Egui.cpp:
Code:
class Egui:public Gtk::Window {
   public:
     Egui (BaseObjectType* cobject, const Glib::RefPtr<Gtk::Builder>& m_refBuilder);
     virtual ~Egui ();

   protected:
   Gtk::Window *mwindow;
   Gtk::Button *f5bttn;   

   void on_f5bttn_clicked ();
};


Top
 Profile  
 
 Post subject: Re: C++, gtkmm and Glade
PostPosted: Thu Dec 01, 2011 11:46 am 
Offline
Familiar Face

Joined: Tue Mar 16, 2010 9:35 pm
Posts: 24
Hm, that seemed to work. Is that all the difference there is?

Here's the new working example:
elevator.cpp:
Code:
#include <gtkmm.h>
#include "Egui.h"

using namespace std;

int main (int argc, char *argv[]) {
   Glib::RefPtr<Gtk::Builder> builder;
     Gtk::Main kit(argc, argv);
   
     // load the interface
   builder = Gtk::Builder::create();
   builder->add_from_file("src/Egui.glade");

     // Get the main window:
     Egui *mwindow;
     builder->get_widget_derived("mwindow", mwindow);
   
     // start the event loop
     Gtk::Main::run(*mwindow );
   
     return 0;
}

Egui.cpp:
Code:
#include "Egui.h"
#include <iostream>

using namespace std;

Egui::Egui (BaseObjectType* base_object, const Glib::RefPtr<Gtk::Builder>& m_refBuiler):Gtk::Window(base_object) {
   m_refBuiler->get_widget("mwindow", mwindow);
   m_refBuiler->get_widget("f5bttn", f5bttn);
   
   // Connect the appropriate callbacks...
     f5bttn->signal_clicked().connect(sigc::mem_fun(*this, &Egui::on_f5bttn_clicked));   // signal_clicked -> in the glade file: clicked

}

Egui::~Egui () {}

// Signal handler methods:
void Egui::on_f5bttn_clicked () {
   cout << "Mmmmm.....5......" <<endl;
}

Egui.h:
Code:
#ifndef EGUI_WINDOW_H
#define EGUI_WINDOW_H

#include <gtkmm.h>

class Egui:public Gtk::Window {
   public:
   Egui (BaseObjectType* cobject, const Glib::RefPtr<Gtk::Builder>& m_refBuilder);
     virtual ~Egui ();

   protected:
   Gtk::Window *mwindow;
   Gtk::Button *f5bttn;   

   void on_f5bttn_clicked ();
};

#endif // EGUI_WINDOW_H


Top
 Profile  
 
 Post subject: Re: C++, gtkmm and Glade
PostPosted: Thu Dec 01, 2011 6:23 pm 
Offline
Never Seen the Sunlight

Joined: Mon Apr 28, 2008 5:52 am
Posts: 777
Location: UK
Libglademm and Gtk::Builder are very similar, but there are some differences, but it is now better to use Gtk::Builder as that is all that is available for GTK version 3.

I would also avoid "using namespace" as this could cause problems for you in the future.

_________________
E.


Top
 Profile  
 
 Post subject: Re: C++, gtkmm and Glade
PostPosted: Fri Dec 02, 2011 4:38 pm 
Offline
Familiar Face

Joined: Tue Mar 16, 2010 9:35 pm
Posts: 24
Hm, I should maybe use Glib instead? And just specify it directly if I need things from the standard library?


Top
 Profile  
 
 Post subject: Re: C++, gtkmm and Glade
PostPosted: Fri Dec 02, 2011 8:18 pm 
Offline
Never Seen the Sunlight

Joined: Mon Apr 28, 2008 5:52 am
Posts: 777
Location: UK
What I mean is do not use this
Code:
using namespace std;
...
  cout << "Hello world\n";
...

but use
Code:
...
   std::cout << "Hello world\n";
...


Suppose you have another library called DataObjectLib and that contained a class called list. If you used
Code:
using namespace std;
using namespace DataObjectLib;

Your compiler will not be able to know which list class to use std::list or DataObjectLib::list.

_________________
E.


Top
 Profile  
 
 Post subject: Re: C++, gtkmm and Glade
PostPosted: Fri Dec 02, 2011 10:18 pm 
Offline
Familiar Face

Joined: Tue Mar 16, 2010 9:35 pm
Posts: 24
Yah, that's what I meant by "specify it directly". Thanks - good point :-)


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 9 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 3 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group