Re: Must widgets stay in context?



Exactly. You have a choice: you manage the lifetime of your widgets
(your own heap management or via the stack), or let GTK do it. :-)

A question regarding this: does GTK have some sort of
garbage-collection management, or are all of the "manage()"ed widgets
finally freed at runtime?

On 12/14/05, rob page <page rob gmail com> wrote:
> I believe you want to be using Gtk::manage
>
> advbox->add(*Gtk::manage(new class Gtk::Label(_("Misc Settings"))));
>
> (code ripped from one of my programs)
>
> which should be much easier to use dynamically.
>
> Rob.
>
> On 14/12/05, Karen Pease <meme daughtersoftiresias org> wrote:
> > Hi - I'm new to gtkmm, and still just starting to get my feet wet.  I'm a bit
> > confused by something and can't find information in the documentation about
> > it - namely, must widgets stay in context after they're added to a container?
> > gtkmm forbids, say:
> >
> > some_frame.add(Gtk::Label("Test"));
> >
> > because it wants a Gtk::Label&, not a Gtk::Label.  Most sample code that I've
> > seen have a member variable (say, m_label_test) used like:
> >
> > SomeClass::SomeClass() : m_label_test("Test")
> > {
> >    some_frame.add(Gtk::Label("Test"));
> > }
> >
> > This seems to suggest to me that gtk cares whether the widget goes out of
> > context or not - that it's not copying the widget that I gave it, but
> > referencing it by pointer.  And thus, it would seem to suggest that this is
> > unacceptable:
> >
> > some_function()
> > {
> >     Gtk::Label label_test("Test");
> >     some_frame.add(label_test);
> > }
> >
> > If I am correct in this, then how do I deal with a situation in which what
> > widgets are used is dynamic?  In my application, I have an information frame
> > which can display information about any one of a wide range of possible
> > objects in a scene, and each object will display different kinds of
> > information about itself (it's not the same type of information between
> > different objects - a wire may list its current, voltage, resistance,
> > conductor material, conductor diameter, etc; a vehicle may list its velocity,
> > wind resistance, drag coefficient, G-forces, mass, etc).
> >
> > Do I have to have a member variable widget for every last label possibility
> > even though at any given time the vast majority of them will be unused?  That
> > would be a pain, at least as far as labels go.  I tried creating
> > std::vector<Gtk::Label> label_list to store any widgets that got created, but
> > that didn't work.  Gtk::Label's copy constructor is private, so
> > label_list.push_back(Gtk::Label(text)) fails.  Right now I'm using a
> > roundabout hack, using a std::vector<Gtk::Label*>, filling it with
> > label_list.push_back(new Gtk::Label(text)), deferencing the pointers when I
> > add them to the frame, and using a cleanup function that deallocates
> > everything in label_list when it is no longer used.  Needless to say, this is
> > ugly.  Is there a better way?
> >
> > Thanks for your help!
> >
> >         - Karen
> >
> > _______________________________________________
> > gtkmm-list mailing list
> > gtkmm-list gnome org
> > http://mail.gnome.org/mailman/listinfo/gtkmm-list
> >
> _______________________________________________
> gtkmm-list mailing list
> gtkmm-list gnome org
> http://mail.gnome.org/mailman/listinfo/gtkmm-list
>


--
R. Douglas Barbieri
doug dooglio net



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]