Can gtkmm benefit from GtkBuilder's <template> syntax?

I’m working with nightly gtkmm4 , so windows / custom widgets are now C++ classes. Also I’m writing .ui xml manually.

I wonder if it’s possible to bind <template> from a .ui file to those C++ class like we did in C, such that almost no widget assembling code is required.

If not, do we just assign id to interesting <object>s, fetching them via Gtk::Builder::get_widget(), and install one by one as class’s data members (maybe in the ctor’s member-init-list)? Cause I find it a bit annoying in doing things like

MyWin::MyWin(MyApp *app):Gtk::ApplicationWindow(app){ 
    auto builder = Gtk::Builder::create_from_resource("/org/myproj/MyWin.ui");
    auto menu_button = builder->get_widget<Gtk::MenuButton>("server_group_menu_button");
    auto menu_model = builder->get_object<Gio::MenuModel>("server_group_menu");
    // would be nice if <child type="titlebar"> is around!
    auto header_bar = builder->get_widget<Gtk::HeaderBar>("header_bar");

    // and many more widget assembling code ...


Okay I found a good reference: examples/book/builder/derived · master · GNOME / gtkmm-documentation · GitLab

So I guess the answer is no . Don’t use <template> in UI XML with gtkmm project. gtkmm expects you to use Gtk::Builder::get_widget for vanilla widget within Gtk:: namespace, and Gtk::Builder::get_widget_derived (note that this one is static member func - weird API design I bet) for custom widgets.

The idea is that - don’t create instances of custom widget programmatically in your code when using gtkmm - write your custom MyWin top level window as a plain top-level <object> in some .ui file, and let Gtk::Builder create the whole thing for you, which also means that API like Gtk::Application::make_window_and_run isn’t as useful.

Also follow the instruction here - they are up-to-date for gtk 4.10: Using derived widgets

See my comment here, you can do that but you have to call the C functions:

I am sure a motivated person could make some nice wrappers for these, and also figure out how to get template children/callbacks to work.

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.