GTK+ Forums

Discussion forum for GTK+ and Programming. Ask questions, troubleshoot problems, view and post example code, or express your opinions.
It is currently Fri Apr 18, 2014 3:25 am

All times are UTC




Post new topic Reply to topic  [ 2 posts ] 
Author Message
 Post subject: About gtkmm and Widget allocation
PostPosted: Tue Sep 08, 2009 12:44 pm 
Offline

Joined: Tue Sep 08, 2009 12:30 pm
Posts: 1
Hi all,

I'm having some issues in developing with gtkmm.
Usually all the containers want a ref to a Gtk::Widget object.
That makes sense, it works and all those things, but it forces you to keep the objects on the heap*.
As far as I understand, when any Gtk::Widget goes out of scope (eg is destructed) it will notify the parent object and basically will be removed from the GUI.
The problem is when you have to populate containers (like Gtk::Notebook) on the fly, with dynamic data.
Naturally, if you use this approach
Code:
_nb.append_page(*(new Gtk::Label("test")), "label");

it works, as seen as the object is never destroyed.
In case I have to replace elements of a container such allocated I can just do the following:
Code:
Glib::ListHandle<Widget*>    c_list = nb.get_children();
for(Glib::ListHandle<Widget*>::iterator it = c_list.begin(); it != c_list.end(); ++it)
     delete *it;

The point is that this way the destructors should be automatically called, the parent container notified, and memory then freed as well.

I don't like this approach because is not exception safe and is definitely not elegant.

Is there any better solution/more elegant way to do all this dynamic gui generation?

Cheers,

*Basically the children Gtk::Widget have to be allocated during the whole lifetime of the parent container. Naturally objects on the stack work as well as long the parent container is destroyed before they are.


Top
 Profile  
 
 Post subject:
PostPosted: Sun Sep 13, 2009 9:05 am 
Offline
Never Seen the Sunlight

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

I presume that you want to hand over widget destruction to the parent. The way to do this for objects derived from Gtk::Object (this nearly all Gtkmm widgets) is to use the Gtk::manage() template. You can still delete widgets if you need to as Gtk::manage() sets up a reference system to make memory allocation/deallocation a little easier.

Code:
template <class T>
T* Gtk::manage ( T *     obj)


This is the code example from the GTKMM manual

Code:
Gtk::Button* button = Gtk::manage( new Gtk::Button("Hello") );
vbox.pack_start(*button); //vbox will delete button when vbox is deleted.



E.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 2 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