Get property value from a composite widget template.ui file

I am struggling already for a while now on how to get the property of composite widget set using vala and custom property added to xml builder template. It did work a couple of times, but dont know what i have changed, but it does not work anymore. And I think my composite widget is missing something. Am i correct, that instancing the widget using a xml builder template, should be enough?

Without calling the widget in code i get this error:

Blockquote Error building template class ‘TimewidgetWindow’ for an instance of type ‘TimewidgetWindow’: .:0:0 Invalid object type ‘TimewidgetCustomSpinButton’

When i call the composite widget using [GtkChild] i get no errors, but my customproperty keeps having a default value, and i suspect that if i cal [GtkChild] its creating a new isntance and overwrite the instance of my xml file so the value is reset to back to default value. I have been binding the template usng bind_tempalte_child_full etc, but whetever i try, the customproperty value stays default and never gets set. But if i add a debug message to the set function. I can see the value of the propert, but it never reaches the getter…

When i create a curstom set function, i can see the value of my property inside my set fucntion, but if i parse into a variable, the getter wont have that same value… So what am i missing?. I read tons of tutorials, watched code from others, but i keep having this same issue… I have no clue what i am doing wrong.

<?xml version="1.0" encoding="UTF-8"?>
<interface>
  <requires lib="gtk" version="4.0"/>
  <template class="TimewidgetCustomSpinButton" parent="GtkWidget">
    <child>
      <object class="GtkSpinButton" id="spin_button"></object>
    </child>
  </template>
</interface>

Then i start the instance in window.ui file using TimewidgetCustomSpinButton.

<?xml version="1.0" encoding="UTF-8"?>
<interface>
  <requires lib="gtk" version="4.0"/>
  <template class="TimewidgetWindow" parent="GtkApplicationWindow">
    <property name="default-width">600</property>
    <property name="default-height">300</property>
    <child type="titlebar">
      <object class="GtkHeaderBar" id="header_bar">
        <child type="end">
          <object class="GtkMenuButton">
            <property name="icon-name">open-menu-symbolic</property>
            <property name="menu-model">primary_menu</property>
          </object>
        </child>
      </object>
    </child>
    <child>
      <object class="GtkBox" id="main">
        <child>
          <object class="GtkLabel" id="label">
            <property name="label">Hello, World!</property>
            <style>
              <class name="title-1"/>
            </style>
          </object>
        </child>
        <child>
          <object class="TimewidgetCustomSpinButton" id="spin_button">
            <property name="customproperty">some value</property>
          </object>
        </child>
      </object>
    </child>
  </template>
  <menu id="primary_menu">
    <section>
      <item>
        <attribute name="label" translatable="true">_Preferences</attribute>
        <attribute name="action">app.preferences</attribute>
      </item>
      <item>
        <attribute name="label" translatable="true">_Keyboard Shortcuts</attribute>
        <attribute name="action">win.show-help-overlay</attribute>
      </item>
      <item>
        <attribute name="label" translatable="true">_About TimeWidget</attribute>
        <attribute name="action">app.about</attribute>
      </item>
    </section>
  </menu>
</interface>

This is my composite widget, that has the property named customproperty.

using Gtk;

namespace Timewidget {

    [GtkTemplate (ui = "/dev/esocoding/timewidget/spinbutton/customspinbutton.ui")]
    public class CustomSpinButton : Widget {

        public string customproperty {set;get;default="default";}

        static construct {
            set_layout_manager_type (typeof (BoxLayout));
        }

        construct {
            message ("construct of customspinbutton customproperty has value = " + this.customproperty);
        }

        public CustomSpinButton() {
            message ("main function");
        }

        public override void dispose () {
            base.dispose ();
        }

    }

}

And finally my window.vala.

using Gtk;

namespace Timewidget {
    [GtkTemplate (ui = "/dev/esocoding/timewidget/window.ui")]
    public class Window : Gtk.ApplicationWindow {
        


        public Window (Gtk.Application app) {
            Object (application: app);
   
        }
    }
}

A weird discovery, if instead of set i use a set construct, it works… Still wondering why the usual method with
property { set; get; default=""; } does not work.

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