Search code examples
c++gtkmm

How to get parent Gtk::Window from inside derived widget class?


I have a class inheriting from Gtk::Box so I can create multiple instances of it at runtime and add them dynamically to a Gtk::Notebook.

However if there is a critical error, I want to pop up a message that won't disappear behind the main window, so I need to get the parent window from within my derived Gtk::Box class.

Going off the question here (which is for GTK, not for gtkmm), I have done this:

Gtk::Container *parent = this->get_toplevel();
if (parent->get_is_toplevel()) {
    Gtk::MessageDialog dlg(*parent, "blah");
    dlg.run();
}

However I get an error telling me there is "no known conversion from Gtk::Container to Gtk::Window".

Am I supposed to use a dynamic_cast<> to typecast the Gtk::Container into a Gtk::Window? If so, is it unnecessary to call get_is_toplevel()? (Because that's kind of the same as checking the result of the dynamic_cast<>).

Or is there a different way this should be done in gtkmm?


Solution

  • Well, this works, but I've no idea whether it's correct or not:

    Gtk::Window *parent = dynamic_cast<Gtk::Window *>(this->get_toplevel());
    if (parent) {
        Gtk::MessageDialog dlg(*parent, "blah");
        dlg.run();
    }