Re: adding a container to a widget in Gtkmm



Hi Charles,

On Thu, 23 Dec 2010 14:49:39 -0600 you wrote:
> Ok, if buttons[x] is a widget pointer and vertical_box is a VBox pointer how
> can I add the vertical_box to the widget ?
> 

What do you mean by that? Do you mean you have declared:

  Gtk::Widget * buttons[5];

or such like?

> 
> I can do this in GTK+ with:
> 
> gtk_container_add(GTK_CONTAINER(buttons[x]), vertical_box);
> 

Yes, because GTK+ is for C, where class inheritance doesn't exist and
the Glib framework forced you to type-cast the pointer at the point of
use.

> 
> buttons[x] (the widget pointer) does not seem to want to give me an add
> operation.

If it's defined as I postulated above then no, it won't, because a
Gtk::Widget does not have an "add" method. You need to be using a
Gtk::Container for that.

> 
> Is there another command that could be used for this purpose ?
> 
Probably what you need to do is define the array right. In GTK+ for C
it was necessary to pass pointers to widgets for an awful lot of
functions, so you're in the habit of declaring your variables as
pointers to widgets, because it reduces the type casting. With GTKmm
you have a compiler that understands inheritance, and it's no longer
necessary to typecast to a parent type. Therefore you should define:

  Gtk::Button * buttons[5];

When you do so, you will find that 

  buttons[x]->add ( vertical_box );

becomes valid, because a Gtk::Button is derived from Gtk::Container
(through another class).

Cheers,
Rob


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