Search code examples
c++treeviewglibgtkmm

How to downcast RefPtr


In Gtk I have a class hierarchy like this:

Gtk::ListStore is derived from Gtk::TreeModel

From a Gtk::TreeView I can get with get_model() a Glib::RefPtr<Gtk::TreeModel>

If I use in my Gtk::Treeview a Gtk::ListStore as a Gtk::TreeModel and call get_model()I get a Glib::RefPtr< TreeModel >

But I want call member functions of Gtk::ListStore.

How can I cast the Glib::RefPtr<Gtk::TreeModel> down to Glib::RefPtr<Gtk::ListStore>. Is there a standard way or is there a hack needed or how is the typical procedure to act with views and different store types. Are there any checking functions so that the upcast can done safe, maybe with compile time checking or if not possible during runtime? Any hint to any documentation.


Solution

  • For downcasting use

    Glib::RefPtr<Gtk::ListStore> ptrDerived = Glib::RefPtr<Gtk::ListStore>::cast_dynamic(treeModel);
    

    documentation