Can GtkEntry be bound to a gchar* property?

Imagine you have a GtkEntry on a form:

<?xml version="1.0" encoding="UTF-8"?>
<interface>
  <requires lib="gtk" version="4.0"/>
  <template class="GtkEntryWnd" parent="GtkApplicationWindow">
    <property name="default-width">320</property>
    <property name="default-height">320</property>
    <child>
      <object class="GtkBox">
        <property name="orientation">vertical</property>
        <child>
          <object class="GtkBox">
            <property name="homogeneous">1</property>
            <child>
              <object class="GtkLabel">
                <property name="label">Friendly name</property>
              </object>
            </child>
            <child>
              <object class="GtkEntry" id="displayName">
                <!-- <binding ??? -->
              </object>
              ...

image

And in your GtkEntryWnd

struct _GtkEntryWnd
{
	GtkApplicationWindow parent_instance;
	gchar* displayName;
};

you’ve registered “DisplayName” property in the following way:

properties[PROP_DISPLAY_NAME] = g_param_spec_string("DisplayName", "DisplayName",
		"Friendly name", NULL, (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));

Can a text shown by GtkEntry be bound to the value of “DisplayName”?

Use g_object_bind_property() to bind the text property on the GtkEntry to the DisplayName property on your class.

1 Like

Thanks a lot!

Is there a way to specify this binding in the .ui markup?

Yes, you can define property bindings in UI files; in this case it would look like:

<child>
  <object class="GtkEntry" id="displayName">
    <property name="text"
              bind-source="GtkEntryWnd"
              bind-property="DisplayName"/>
  </object>
</child>

if you want the entry’s contents to display the contents of the DisplayName property; if you want the entry to modify the DisplayName then you can use bind-flags="bidirectional".

Yeah, works like a charm! :slight_smile:

Thank you very much!

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