I'm still trying to learn vala and I am having a problem with GtkButton Signals.
I want to connect a function void refresh ()
to a GtkButton. When the Button is clicked the function should be called and set the Label of a GtkLabel. So I write GtkButton.clicked.connect (this.function);
. This should call the function when I click on the button, right?
My function is very simple for testing and should change the text of a GtkLabel. So I get void function () { GtkLabel.label = "New Text"; }
.
When I test this litte program clicking on the button does nothing or at least I can't see anything.
What am I missing?
Here is my code:
namespace Zeiterfassunggtk {
[GtkTemplate (ui = "/org/gnome/Zeiterfassunggtk/window.ui")]
public class Window : Gtk.ApplicationWindow {
[GtkChild]
Gtk.Button refreshbutton;
Gtk.Button menubuttonrefresh;
void refresh () {
label1.label = "Clicked";
}
public Window (Gtk.Application app) {
Object (application: app);
refreshbutton.clicked.connect (this.refresh);
menubuttonrefresh.clicked.connect (this.refresh);
this.show_all ();
}
}
}
you can look at the full code at github.com
You need [GtkChild]
on every field if they are in the template. Right now, menurefresh
contains null and won't be connected to anything. label1
is also null, so changing its label won't do anything.
The correct code is:
namespace Zeiterfassunggtk {
[GtkTemplate (ui = "/org/gnome/Zeiterfassunggtk/window.ui")]
public class Window : Gtk.ApplicationWindow {
[GtkChild]
Gtk.Button refreshbutton;
[GtkChild]
Gtk.Button menubuttonrefresh;
[GtkChild]
Gtk.Label label1;
void refresh () {
label1.label = "Clicked";
}
public Window (Gtk.Application app) {
Object (application: app);
refreshbutton.clicked.connect (this.refresh);
menubuttonrefresh.clicked.connect (this.refresh);
this.show_all ();
}
}
}