Handle event from .ui file

I tried to handle events from .ui file with add_callback_symbol in vala but I get this error

error: Argument 2: Cannot convert from `void on_button2_clicked (Gtk.Button)' to `void GLib.Callback ()'
        builder.add_callback_symbol ("on_button2_clicked", on_button2_clicked);

this is event in .ui file

<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"/>

and this is handle code

// out main
public void on_button2_clicked (Button source) {
    source.label = "Thanks!";

// in main
var builder = new Builder ();
        builder.add_from_resource ("/me/rush/Rush/main.ui");
        builder.add_callback_symbol ("on_button2_clicked", on_button2_clicked);

I tried to use function as lambda expression in place of callback it compile successfully but no thing work, also I cant pass parameters in this way (if I pass error appear)

so please if anyone know help me, coz I tried also connect_signals from this example but also it doesnt work with me, its appear error tell me to use -rdynamic when compiling from searching for this error mean I need to use gmoudle and it need editing when I compile for windows (I use debian), so I think it easiet to know how to do it with add_callback_symbol or add_callback_symbols

to work with callback from .ui file i solved proplem with connect_signals
I found solution here

Now I see connect_signals better than add_callback_symbol and designed better with OOP way in coding

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:


this even better than connect_signal
and thank you for your active interaction I also saw your reply in StackOverflow really thank you :heart:

1 Like

You’re welcome :wink: Happy hacking!


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