Re: Do widgets have to be members of class?



On Mon, 2009-09-14 at 20:00 -0700, hokie54 wrote:
> Forgive me for my ignorance, but I'm new to any sort of GUI development...
> 
> I'm attempting to create a table with X labels inside of it.  However, it
> seems as if I can not add items to the table that are not members of the
> class.  For example this will NOT work:
> 
> MainWindow.h
> public:
> 	MainWindow();
> 	
> protected:
> 	Gtk::Table my_table;
> };
> 
> MainWindow.cc
> MainWindow::MainWindow() :
> my_table(2,2, true),
> {
> 	add(my_table);
>         Gtk::Label label("LABEL");
> 
> 	my_table.attach(label, 0, 1, 0, 1);
> 
> 	show_all_children();
> }
> 
> 
> It seems as if making 'label' a protected member of MainWindow is the only
> way to make this work.  Is there any way around this since I do not
> initially know how many labels I will need inside the table?
> 
> Thanks

Your label in constructor has local scope, so after constructor finishes
its activity, label is destroyed. So yes, it has to be a member of your
class. Or have global scope, which is ugly.

If you don't know how many label will be inside, then use a vector or
list containing labels.

Krzem



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