Search code examples
gtk3vala

manage signals in GTK .ui files in vala


from the following .ui files, I have the signal :

     <object class="GtkButton" id="button2">
        <property name="label">Button 2</property>
        <property name="visible">True</property>
        <property name="can_focus">False</property>
        <property name="receives_default">False</property>
        <signal name="clicked" handler="on_button2_clicked" swapped="no"/>
      </object>

the name of signal on_button2_clicked and the following code I used to handle event by add_callback_symbool :

    // Create Builder
    var build = new Gtk.Builder ();
    build.add_from_resource ("/me/rush/Rush/main.ui");

    // Create an instance
    var window =  (Gtk.ApplicationWindow) build.get_object ("window");

    // handle event
    build.add_callback_symbol ("on_button2_clicked", ()=> {print ("click");});

    window.application = app;
    window.show_all ();

GUI appeard in screen but handling of event on_button2_clicked doesnt work, I tried to handle event with build.connect_signals (null) and defiend function with same name but also doesnt work

when googling I found a way to work with callbaks in templates, but what I want to do is handle event with connect.signals or add_callback_symbol also I tried example here but it doesnt work


Solution

  • I also replied this to your question on GNOME's discourse, but I'll post the answer again here for visibility:

    It's maybe good to know that Vala has built-in support for GtkTemplates, which means you don't need to explicitly call the GtkBuilder API anymore. Vala will do the necessary checks for you (at compile-time even).

    You can find multiple examples for this in the GNOME Contacts repository, but to give a concrete one, let's show the SetupWindow: