好吧,我基本上是Gtk中的新手,我将一个结构作为参数发送给回调函数,该函数将在用户单击按钮时启动。当我试图获得它的值时,它没有正确填充它我不知道为什么。
计数器数据结构具有以下内容:
typedef struct{
int flag;
GtkWidget* textview;
GtkWidget* label;
gchar* test;
} Counter_data;
我所说的功能就是:
static void start_counter(GtkWidget *btn, gpointer data){
int counter = 59;
gchar* tiempo;
g_print("starting/restarting counter\n");
gtk_button_set_label((GtkButton*)btn, "Started counting");
while(gtk_events_pending()) gtk_main_iteration();
while(1){
sleep(1);
tiempo = get_minutes(counter++);
g_print(tiempo);
g_print("%d\n",((Counter_data*) data)->flag);
/* gtk_label_set_text((GtkLabel* )((Counter_data*) data)->label, "testing2"); */
while(gtk_events_pending()) gtk_main_iteration();
}
}
在我的主要流程中,我把它放在:
Counter_data counter_data;
window = gtk_application_window_new (app);
gtk_window_set_title (GTK_WINDOW (window), "Pomodoro 0.1");
gtk_window_set_default_size (GTK_WINDOW (window), ((Screen_size *) user_data)->x, ((Screen_size *) user_data)->y );
parentBox = gtk_box_new(FALSE, 0);
btn_box = gtk_button_box_new(GTK_ORIENTATION_HORIZONTAL);
textview = gtk_text_view_new();
label_time = gtk_label_new(NULL);
counter_data.flag = 0; //we are just starting the counter.
counter_data.textview = textview;
counter_data.label = label_time;
counter_data.test = "testing...";
gtk_box_pack_start(GTK_BOX(parentBox), btn_box, TRUE, TRUE, 0);
gtk_box_pack_start(GTK_BOX(parentBox), textview, TRUE, TRUE, 0);
gtk_box_pack_start(GTK_BOX(parentBox), label_time, TRUE, TRUE, 0);
btn = gtk_button_new_with_label("Start counting");
g_signal_connect(btn, "clicked", G_CALLBACK(start_counter),&counter_data);
当我在控制台中打印flag
的值时g_print("%d\n",((Counter_data*) data)->flag);
它打印垃圾或地址,我不知道了。我的linter说一切都很好:S
答案 0 :(得分:0)
感谢您的帮助,问题是我没有使用malloc分配空间
Counter_data *counter_data = malloc(sizeof *counter_data);
感谢您的帮助!