开发者

passing argument/arguments signal to callback on GTK+ in C

Here is my code:

#include <gtk/gtk.h>

G_MODULE_EXPORT void waka(GtkWidget *button, GtkWidget* entry1)
{
printf("%s",gtk_entry_get_text(GTK_ENTRY(entry1)));
}

int main(int argc, char * argv[])
{

GtkWidget *window, *button, *entry;

gtk_init(&argc,&argv);

window = gtk_window_new(GTK_WINDOW_TOPLEVEL);

button = gtk_button_new_with_label("hha");
g_signal_connect(G_OBJECT(button),"clicked",G_CALLBACK(waka),entry);

entry = gtk_entry_new();

GtkWidget *vbox;

vbox = gtk_vbox_new(FALSE,2);

gtk_box_pack_start_defaults(GTK_BOX(vbox),button);
gtk_box_pack_start_defaults(GTK_BOX(vbox),entry);

gtk_container_add(GTK_CONTAINER(window),vbox);


gtk_widget_show_all(window);
gtk_main();


return 0;
}

When I launch it, and try to fill in some string in the entry box, and then click the button, it said 'segmentation fault'. What is going on?

Also, the callback only works with one user data argument. How about if I want two or more argum开发者_开发知识库ents, what must I do in the callback function, and in the call to g_signal_connect()?


the problem is that you're trying to use pointer to entry before initializing it. I've changed a bit your code to fix this, see if it will work for you:

#include <gtk/gtk.h>

void waka(GtkWidget *button, GtkWidget* entry1)
{
 g_print("entry: %s\n", gtk_entry_get_text(GTK_ENTRY(entry1)));
}

int main(int argc, char * argv[])
{
 GtkWidget *window, *button, *entry, *vbox;

 gtk_init(&argc, &argv);

 window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
 button = gtk_button_new_with_label("hha");
 entry = gtk_entry_new();
 vbox = gtk_vbox_new(FALSE, 2);

 gtk_box_pack_start_defaults(GTK_BOX(vbox),button);
 gtk_box_pack_start_defaults(GTK_BOX(vbox),entry);

 gtk_container_add(GTK_CONTAINER(window),vbox);

 g_signal_connect(G_OBJECT(button), "clicked", G_CALLBACK(waka), entry);

 gtk_widget_show_all(window);
 gtk_main();

 return 0;
}

hope this helps, regards

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜