Accessing widgets
| Chapter 23. Glade and Gtk::Builder |

## Accessing widgets

To access a widget, for instance to `show()` a dialog, use the `get_widget()` method, providing the widget's name. This name should be specified in the Glade Properties window. If the widget could not be found, or is of the wrong type, then the pointer will be set to 0.

    Gtk::Dialog* pDialog = 0;
    builder->get_widget("DialogBasic", pDialog);

Gtk::Builder checks for a null pointer, and checks that the widget is of the expected type, and will show warnings on the command line about these.

Remember that you are not instantiating a widget with `get_widget()`, you are just obtaining a pointer to one that already exists. You will always receive a pointer to the same instance when you call `get_widget()` on the same `Gtk::Builder`, with the same widget name. The widgets are instantiated during `Gtk::Builder::create()`.

`get_widget()` returns child widgets that are `manage()`ed (see the Memory Management chapter), so they will be deleted when their parent container is deleted. So, if you get only a child widget from Gtk::Builder, instead of a whole window, then you must either put it in a `Container` or delete it. `Windows` (such as `Dialogs`) cannot be managed because they have no parent container, so you must delete them at some point.

### Example

This simple example shows how to load a Glade file at runtime and access the widgets with Gtk::Builder.

Source Code

File: `main.cc`

    #include <gtkmm.h>
    #include <iostream>

    Gtk::Dialog* pDialog = 0;

    static
    void on_button_clicked()
    {
      if(pDialog)
        pDialog->hide(); //hide() will cause main::run() to end.
    }

    int main (int argc, char **argv)
    {
      Gtk::Main kit(argc, argv);

      //Load the GtkBuilder file and instantiate its widgets:
      Glib::RefPtr<Gtk::Builder> refBuilder = Gtk::Builder::create();
    #ifdef GLIBMM_EXCEPTIONS_ENABLED
      try
      {
        refBuilder->add_from_file("basic.ui");
      }
      catch(const Glib::FileError& ex)
      {
        std::cerr << "FileError: " << ex.what() << std::endl;
        return 1;
      }
      catch(const Gtk::BuilderError& ex)
      {
        std::cerr << "BuilderError: " << ex.what() << std::endl;
        return 1;
      }
    #else
      std::auto_ptr<Glib::Error> error;

      if (!refBuilder->add_from_file("basic.ui", error))
      {
        std::cerr << error->what() << std::endl;
        return 1;
      }
    #endif /* !GLIBMM_EXCEPTIONS_ENABLED */

      //Get the GtkBuilder-instantiated Dialog:
      refBuilder->get_widget("DialogBasic", pDialog);
      if(pDialog)
      {
        //Get the GtkBuilder-instantiated Button, and connect a signal handler:
        Gtk::Button* pButton = 0;
        refBuilder->get_widget("quit_button", pButton);
        if(pButton)
        {
          pButton->signal_clicked().connect( sigc::ptr_fun(on_button_clicked) );
        }

        kit.run(*pDialog);
      }

      return 0;
    }

Chapter 23. Glade and Gtk::Builder |  | Using derived widgets