Custom widget with GtkConstraintLayout in UI file

hi

i’d like to use the GtkConstraintLayout as a layout manager in my custom widget with constraints defined in UI file

so i’m subclassing GtkWidget and do

gtk_widget_class_set_layout_manager_type(widget_class, GTK_TYPE_CONSTRAINT_LAYOUT);

from the documentation i see that GtkContraintLayout is also buildable but the example there is somewhat confusing to me

  <object class="GtkConstraintLayout">
    <constraints>
      <constraint target="button" target-attribute="start"
                  relation="eq"
                  source="super" source-attribute="start"
                  constant="12"
                  strength="required" />
      <constraint target="button" target-attribute="width"
                  relation="ge"
                  constant="250"
                  strength="strong" />
    </constraints>
  </object>

as far as i understand my widget itself uses constraint layout manager, so i expected to be able to use <constraints> tag inside UI, but gtk says this tag is unhandled

my question is: how i can use constraints in UI files? where to put child and constaint tags themselves?
a small example would be nice

thanks

gtk4, c

ok, i think i’ve figured it out
i should use <property name="layout-manager"> for that to work

<template class="CustomWidget" parent="GtkWidget">
  <child>
    ...
  </child>
  <property name="layout-manager">
    <object class="GtkConstraintLayout">
      <constraints>
        ...
      </constraints>
    </object>
  </property>
</template>

also, seems like in that case gtk_widget_class_set_layout_manager_type(widget_class, GTK_TYPE_CONSTRAINT_LAYOUT); is not needed anymore

The quick answer is: if you’re setting the layout manager type in the class, then you’re supposed to be constructing the layout yourself, as it’s part of the class and it applies to all instances of the type.

If you’re using a UI description file, then you should use the GtkWidget:layout-manager property, since it applies to a specific instance.

thanks for your answer

could you please clarify
when you say “constructing the layout yourself”, do you mean that i should be doing it in the class code, not in the ui file?

and another thing:
i’ll be adding more widgets dynamically to my original custom widget
assuming that i use UI file as i’ve mentioned in my previous message, could you please tell if this approach is correct:

GtkConstraintLayout *layout = GTK_CONSTRAINT_LAYOUT(gtk_widget_get_layout_manager(GTK_WIDGET(self)));
gtk_constraint_layout_add_constraint(layout,
	gtk_constraint_new(
		new_widget, GTK_CONSTRAINT_ATTRIBUTE_TOP, GTK_CONSTRAINT_RELATION_EQ,
		some_child, GTK_CONSTRAINT_ATTRIBUTE_BOTTOM, 1, 0, GTK_CONSTRAINT_STRENGTH_REQUIRED)
);
gtk_constraint_layout_add_constraint(layout,
	gtk_constraint_new_constant(
		new_widget, GTK_CONSTRAINT_ATTRIBUTE_BOTTOM, GTK_CONSTRAINT_RELATION_EQ,
		SOME_HEIGHT, GTK_CONSTRAINT_STRENGTH_REQUIRED)
);

thank you