Vala, GTKBuilder and Autoconnecting Signal Handlers
Contents
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.
-
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 thethispointer as last argument, rather than first. You’ve to do this becauseBuilder.connect_signalspasses the instance as last parameter (user_data), and by aligning yourthispointer withuser_data, you make sure the correct instance is called. -
Make sure your handler function signature matches what is expected. Ignore the
user_dataparameter of most signals – it is used internally byBuilder.connect_signals(see above point). -
Make sure you pass
--pkg gmodule-2.0to yourvalac. If you’re using Autotools, this means you pass it toVALAFLAGS. -
sure your C compiler is called with
-dynamic-export. I’m told this enabled support fordlopenin the binary produced. -
Mangle your names properly.
Cheese.MainWindow.on_quit()becomescheese_main_window_on_quiton C, and that is what GTKBuilder expects. If you are not sure how your function name is mangled byvalac, checkout the generated.cfile.
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 :)
Author yuvipanda
LastMod 2010-05-30