Using a gtkmm widget
| Chapter 29. Recommended Techniques |

## Using a gtkmm widget

Our examples all tend to have the same structure. They follow these steps for using a `Widget`:

1. Declare a variable of the type of `Widget` you wish to use, generally as member variable of a derived container class. You could also declare a pointer to the widget type, and then create it with `new` in your code. Even when using the widget via a pointer, it's still probably best to make that pointer a member variable of a container class so that you can access it later.

2. Set the attributes of the widget. If the widget has no default constructor, then you will need to initialize the widget in the initalizer list of your container class's constructor.

3. Connect any signals you wish to use to the appropriate handlers.

4. Pack the widget into a container using the appropriate call, e.g. `Gtk::Container::add()` or `pack_start()`.

5. Call `show()` to display the widget.

`Gtk::Widget::show()` lets gtkmm know that we have finished setting the attributes of the widget, and that it is ready to be displayed. You can use `Gtk::Widget::hide()` to make it disappear again. The order in which you show the widgets is not important, but we do suggest that you show the top-level window last; this way, the whole window will appear with its contents already drawn. Otherwise, the user will first see a blank window, into which the widgets will be gradually drawn.

Chapter 29. Recommended Techniques |  | Chapter 30. Contributing