Note: I’m not an expert on Vala, GTK+, or anything else for that matter. If I am off base someplace, comment.

I spent quite a some time today trying to get GTKBuilder to automagically connect all my signals to their handlers. I tripped up at a few places, and so recording them here for posterity.

1. If you are using instance methods (like most of us are), you’ve to decorate your handler function with the attribute [CCode (instance_pos=-1)]. This makes sure that the function is passed the this pointer as last argument, rather than first. You’ve to do this because Builder.connect_signals passes the instance as last parameter (user_data), and by aligning your this pointer with user_data, you make sure the correct instance is called.

2. Make sure your handler function signature matches what is expected. Ignore the user_data parameter of most signals – it is used internally by Builder.connect_signals (see above point).

3. Make sure you pass --pkg gmodule-2.0 to your valac. If you’re using Autotools, this means you pass it to VALAFLAGS.

4. sure your C compiler is called with -dynamic-export. I’m told this enabled support for dlopen in the binary produced.

5. Mangle your names properly. Cheese.MainWindow.on_quit() becomes cheese_main_window_on_quit on C, and that is what GTKBuilder expects. If you are not sure how your function name is mangled by valac, checkout the generated .c file.

If you do all these 5 things right and you’re still having trouble, turn up on #vala at irc.gimp.net and ask the helpful folks there :)