How to use gtk/glade in vala
Asked Answered
P

2

7

I'm trying to make a simple app with glade/gtk/vala. So far I have this:

using Gtk;
class HelloWorldApp : GLib.Object {
    const string UI = "test.glade";
    public Window main_window;

    [CCode (instance_pos = -1)]
    public void on_btn_hello_clicked(Button source) {
    stdout.printf("Hello, world");
    }

    construct {
        Builder builder = new Builder();
        builder.add_from_file(UI);
        main_window = builder.get_object("window1") as Window;
        main_window.destroy.connect(Gtk.main_quit);
        builder.connect_signals(this);
    }
}

class HelloWorld : GLib.Object {
    public static int main(string[] args) {
        Gtk.init (ref args);
        HelloWorldApp h = new HelloWorldApp();
        h.main_window.show_all();
        Gtk.main();
        return 0;
    }
}

When I run this it outputs: (helloworld:22641): Gtk-WARNING **: Could not find signal handler 'on_btn_hello_clicked' but otherwise runs fine apart from the handler not being called

What am I doing wrong?

Parachute answered 18/8, 2010 at 12:13 Comment(1)
There is a good inrtoductory tutorial on how to write GUI apps with Glade and Vala over at demonicode.ghoster.io/… - check it out, it's actually really simple! I was stuck on this for months until I found this tutorial :(Thanatopsis
A
11

You are doing one or more of the following three things wrong:

  1. When you compile a program using Gtk.Builder, you have to add --pkg gmodule-2.0 to your valac command line. (link)
  2. When you place your signal handlers inside a class and/or namespace, you have to add the class and/or namespace name to the handler name in Glade, so you should be connecting to hello_world_app_on_btn_hello_clicked. (link)
  3. If you are on Windows, you also have to add [CCode (cname="G_MODULE_EXPORT on_btn_hello_clicked")]. (link)
Angelia answered 18/8, 2010 at 14:34 Comment(0)
D
1

the live.gnome.org has all those informations, including the Windows quirks: http://live.gnome.org/Vala/GTKSample#Loading_User_Interface_from_XML_File

Donate answered 18/8, 2010 at 16:52 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.