Must widgets stay in context?



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




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