Reputation: 5858
It is rather simple to get a Glib::RefPtr
to any widget by using get_widget
function of Gtk::Builder
, but when it comes to getting other objects the corresponding get_object
function returns Glib::Object
, which is not easily convertable to the needed class (such as Gtk::TreeView
).
What is the appropriate way of doing that?
Upvotes: 1
Views: 733
Reputation: 2123
It would be best to use Glib::RefPtr<TheDerivedype>::cast_dynamic(object)
.
However, Gtk::TreeView (which you mention in your question) is a Gtk::Widget, so you would use get_widget() instead of get_object().
If you meant, Gtk::TreeModel, well, defining GtkTreeModels in Glade, for use in gtkmm C++ code, is something that might work since we added some fixes for that in gtkmm recently: https://bugzilla.gnome.org/show_bug.cgi?id=742637
But it's not something that we generally expect to work - many C++ developers would prefer the static type safety of defining the DataModel structure completely in code, and not relying on a translation between C and C++ types. For instance: https://developer.gnome.org/gtkmm-tutorial/stable/sec-treeview-model.html.en#treeview-model-liststore
Upvotes: 1
Reputation: 5858
Glib::RefPtr has a static template function which allows one to do what is needed. This function is logically called cast_static
.
The sample code can be:
treeStore =
Glib::RefPtr< Gtk::TreeStore >::cast_static( builder->get_object("treestore1") );
Upvotes: 0