How to use GtkGrid in Gtk4 with ui files?

Hi,
So I’m trying to create a little app in Gtk4 to improve my skills.
My problem is I don’t know how to configure the grid in the UI files right.
Here is an example using Glade:

<object class="GtkGrid" id="grid">
    <property name="can-focus">False</property>
    <property name="row-spacing">6</property>
    <property name="column-spacing">6</property>
    <child>
        <object class="GtkButton">
            <property name="label">1</property>
            <property name="can-focus">True</property>
            <property name="receives-default">True</property>
        </object>
        <packing>
            <property name="left-attach">0</property>
            <property name="top-attach">0</property>
        </packing>
    </child>
    <child>
        <object class="GtkButton">
            <property name="label" translatable="yes">2</property>
            <property name="can-focus">True</property>
            <property name="receives-default">True</property>
        </object>
        <packing>
            <property name="left-attach">1</property>
            <property name="top-attach">0</property>
            <property name="width">2</property>
        </packing>
    </child>
</object>

I know that Glade neither support Gtk4 nor it is recommended in general anymore but I couldn’t find any reference how it is right.
It would be nice if in the Gtk4 docs were a example

Thanks

It should be something like that (untested):

<object class="GtkGrid" id="grid">
    <property name="can-focus">False</property>
    <property name="row-spacing">6</property>
    <property name="column-spacing">6</property>
    <child>
        <object class="GtkButton">
            <property name="label">1</property>
            <property name="can-focus">True</property>
            <property name="receives-default">True</property>
            <layout>
                <property name="column">0</property>
                <property name="row">0</property>
            </layout>
        </object>
    </child>
    <child>
        <object class="GtkButton">
            <property name="label" translatable="yes">2</property>
            <property name="can-focus">True</property>
            <property name="receives-default">True</property>
            <layout>
                <property name="column">1</property>
                <property name="row">0</property>
                <property name="column-span">2</property>
            </layout>
        </object>
    </child>
</object>

see the properties in the GtkGridLayout documentation.

1 Like

The example in the GtkWidget as a GtkBuildable section of the API reference literally uses a GtkGrid.

1 Like

First of all, thanks for helping me out

Second
I opened a Merge Request to add an example in the GtkGrid docs

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