我在GTK3上。我已经设置了一个带有glade的GUI和用于顶层窗口的后续处理程序:
关注main.c:
#include <gtk/gtk.h>
static gboolean on_wnd_delete_event(GtkWidget *widget, GdkEvent *event, gpointer data){
g_print ("delete event occurred\n");
return TRUE;
}
static void on_wnd_destroy(GtkWidget *widget, gpointer data){
gtk_main_quit ();
}
int main(int argc, char *argv[]){
GtkBuilder *gtkBuilder;
GtkWidget *window;
gtk_init(&argc, &argv);
gtkBuilder = gtk_builder_new();
gtk_builder_add_from_file(gtkBuilder, "gui.glade", NULL);
window = GTK_WIDGET(gtk_builder_get_object(gtkBuilder, "wnd"));
gtk_builder_connect_signals(gtkBuilder, window);
g_object_unref(G_OBJECT(gtkBuilder));
gtk_widget_show(window);
gtk_main();
return 0;
}
编译时我收到警告:
main.c:3:13: warning: ‘on_wnd_delete_event’ defined but not used [-Wunused-function]
static void on_wnd_delete_event(){
^
main.c:8:13: warning: ‘on_wnd_destroy’ defined but not used [-Wunused-function]
static void on_wnd_destroy(){
^
在运行我定义的事件时,不会被称为。
我错了什么?
答案 0 :(得分:2)
对于gtk_builder_connect_signals
,回调函数不能是静态的,因为builder.c文件需要访问它们。 Error 3 error LNK1104: cannot open file 'gtk-3.lib'
gtk_builder_connect_signals(gtkBuilder, NULL);
代替gtk_builder_connect_signals(gtkBuilder, window);
也是必要的。
答案 1 :(得分:0)
您可以通过这种方式添加回调符号:
gtk_builder_add_callback_symbol
(gtkBuilder,
"on_wnd_delete_event",
G_CALLBACK(on_wnd_delete_event));
您也可以导出符号以使其可用,但是您必须按照此处所述更改编译标志: https://stackoverflow.com/a/27931171/2316442