gtk中按钮输出全局变量不一致
来源: 互联网 发布时间:2016-12-26
本文导语: 程序源代码如下,本人是个gtk初学者, Linux下运行如下程序,出现全局变量count没有赋值给局部变量userdata,尝试调试不成功,虽然下面代码可以换另外些表达方式,但是,这个问题不解决终究是一块心病,故在此求解...
程序源代码如下,本人是个gtk初学者, Linux下运行如下程序,出现全局变量count没有赋值给局部变量userdata,尝试调试不成功,虽然下面代码可以换另外些表达方式,但是,这个问题不解决终究是一块心病,故在此求解。
(在这个论坛中,本人一直为潜水一族,此处为第一次发帖求助,望各路神仙不看分数看问题,给本人指点一二,不胜感激)
#include
gint count = 1;
void on_button_cliked (GtkWidget *button, gpointer userdata)
{
g_print ("你好,这是Hello功能的测试。");
g_print ("%dn", (gint)userdata);
count = count + 1;
// g_print ("%dn",(gint)(gpointer)count);
}
int main (int argc, char *argv[])
{
GtkWidget *window;
GtkWidget *button;
gtk_init (&argc, &argv);
window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
g_signal_connect(G_OBJECT(window), "delete_event",
G_CALLBACK (gtk_main_quit), NULL);
gtk_window_set_title(GTK_WINDOW(window), "Hello 功能实现");
gtk_window_set_default_size (GTK_WINDOW(window), 500, 100);
gtk_window_set_position (GTK_WINDOW(window), GTK_WIN_POS_CENTER);
gtk_container_set_border_width(GTK_CONTAINER(window), 40);
button = gtk_button_new_with_label("按下此按键会在终端上显示一行信息");
g_signal_connect(G_OBJECT(button), "clicked",
G_CALLBACK (on_button_cliked), (gpointer)count);
gtk_container_add (GTK_CONTAINER (window), button);
gtk_widget_show (button);
gtk_widget_show (window);
gtk_main ();
return FALSE;
}
(在这个论坛中,本人一直为潜水一族,此处为第一次发帖求助,望各路神仙不看分数看问题,给本人指点一二,不胜感激)
#include
gint count = 1;
void on_button_cliked (GtkWidget *button, gpointer userdata)
{
g_print ("你好,这是Hello功能的测试。");
g_print ("%dn", (gint)userdata);
count = count + 1;
// g_print ("%dn",(gint)(gpointer)count);
}
int main (int argc, char *argv[])
{
GtkWidget *window;
GtkWidget *button;
gtk_init (&argc, &argv);
window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
g_signal_connect(G_OBJECT(window), "delete_event",
G_CALLBACK (gtk_main_quit), NULL);
gtk_window_set_title(GTK_WINDOW(window), "Hello 功能实现");
gtk_window_set_default_size (GTK_WINDOW(window), 500, 100);
gtk_window_set_position (GTK_WINDOW(window), GTK_WIN_POS_CENTER);
gtk_container_set_border_width(GTK_CONTAINER(window), 40);
button = gtk_button_new_with_label("按下此按键会在终端上显示一行信息");
g_signal_connect(G_OBJECT(button), "clicked",
G_CALLBACK (on_button_cliked), (gpointer)count);
gtk_container_add (GTK_CONTAINER (window), button);
gtk_widget_show (button);
gtk_widget_show (window);
gtk_main ();
return FALSE;
}
|
(gpointer)count);
这个明显要传指针过去,你要写 (gpointer)&count);才行
相应的 on click函数里面的应用也要改一下,
g_print ("%dn", (gint) * userdata);
这个明显要传指针过去,你要写 (gpointer)&count);才行
相应的 on click函数里面的应用也要改一下,
g_print ("%dn", (gint) * userdata);