Gtk.Template with property binding

Hi,

I’ writing a custom widget where actual implementation is done with a Template file.

<?xml version="1.0" encoding="UTF-8"?>
<interface>
    <requires lib="gtk" version="4.0"/>
    <template class="Header_Bar" parent="GtkBox">
        <property name="orientation">horizontal</property>
        <child type="center">
              <object class="GtkLabel" id="time">
                  <property name="label">11:45</property>
              </object>
        </child>
    </template>
</interface>

In the python class a define a new property named time.

And I would now like to bind the ''label** property of the GtkLabel in the template file to the time property of the root object (basically the newly created property of the python class).

Is the possible at all? If yes, what would I need to specify as bind-source?

Thanks for your help.
Martin

In C, the mapping is done via gtk_widget_class_bind_template_child() macro.

Refer https://developer.gnome.org/documentation/tutorials/widget-templates.html for more details.

Hi,

Have you tried using the class name as source?

<property name="label" bind-source="Header_Bar" bind-property="time" bind-flags="sync-create"></property>

From a quick look at the internal gtk code, it seems it may work, but I didn’t test.

EDIT: see example here: gtk/inspector/recorder.ui · main · GNOME / gtk · GitLab

That worked perfectly.
Thanks!

1 Like

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