将参数/参数信号传递给 GTK 上的回调;在C中

发布于 2024-10-15 01:45:59 字数 881 浏览 0 评论 0原文

这是我的代码:

#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;
}

当我启动它并尝试在输入框中填写一些字符串,然后单击按钮时,它显示“分段错误”。到底是怎么回事?

此外,回调仅适用于一个用户数据参数。如果我想要两个或更多参数,我必须在回调函数和调用 g_signal_connect() 中做什么?

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 arguments, what must I do in the callback function, and in the call to g_signal_connect()?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

明月夜 2024-10-22 01:45:59

问题是您尝试在初始化之前使用指向entry的指针。我改变了你的代码来解决这个问题,看看它是否适合你:

#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;
}

希望这有帮助,问候

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文