Search code examples
buttonbuildergnomevala

How to connect GtkModelButton


Helo,

I've got the following .ui file:

<child>
  <object class="GtkModelButton" id="button_neworder">
    <property name="can_focus">True</property>
    <property name="text" translatable="yes">Neuer Auftrag</property>
    <property name="action-name">?</property>
    <property name="visible">True</property>
  </object>
</child>

And the following ApplicationWindow:

public class Window : Gtk.ApplicationWindow {
    [GtkChild]

    private unowned Gtk.ListBox listbox_jobs;
    private unowned Gtk.Button button_neworder;

    public Window (Gtk.Application app) {
        Object (application: app, title: "Auftragsverwaltung");
        this.button_neworder.clicked.connect(this.method);
    }

    public void method() {
    }
}

How can I create a clicked signal with GtkModelButton?

The code

this.button_neworder.clicked.connect(this.method);

doesnt work.


Solution

  • You need to add your signal to the .ui file with <signal/>:

    ...
    <child>
      <object class="GtkModelButton" id="button_neworder">
        <property name="can_focus">True</property>
        <property name="text" translatable="yes">Neuer Auftrag</property>
        <property name="action-name">?</property>
        <property name="visible">True</property>
        <signal name="clicked" handler="button_neworder_clicked" />
      </object>
    </child>
    ...
    

    and identify the method as a callback with [GtkCallback]:

    [GtkTemplate(ui = "/template/window.ui")]
    public class Window : Gtk.ApplicationWindow {
        [GtkChild]
    
        private unowned Gtk.ListBox listbox_jobs;
        private unowned Gtk.Button button_neworder;
    
        public Window (Gtk.Application app) {
            Object (application: app, title: "Auftragsverwaltung");
            this.button_neworder.clicked.connect(this.method);
        }
    
        [GtkCallback]
        public void button_neworder_clicked(Gtk.ModelButton button) {
        }
    }