我正在尝试将对象传递给“clicked”事件的回调,以便设置目标标签的文本。
到目前为止,这是我的代码:
#include <gtk/gtk.h>
typedef struct {
int i;
GtkWidget *target;
} Data;
void change( GtkWidget *widget,
Data *data )
{
gtk_label_set_text(GTK_LABEL(data->target), "it did!");
}
int main( int argc,
char* argv[] )
{
gtk_init(&argc, &argv);
GtkWidget *window, *label, *button;
Data data;
data.i = 0;
data.target = label;
window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
gtk_window_set_title(GTK_WINDOW(window), "GTKdemo");
g_signal_connect(window, "delete-event", G_CALLBACK(gtk_main_quit), NULL);
GtkGrid *grid = gtk_grid_new();
button = gtk_button_new_with_label("Click here");
g_signal_connect(button, "clicked", G_CALLBACK(change), &data);
gtk_grid_attach(grid, button, 0,0,1,1);
label = gtk_label_new("this will change");
gtk_grid_attach(grid, label, 0,1,1,1);
gtk_container_add(GTK_CONTAINER(window), GTK_WIDGET(grid));
gtk_widget_show_all(window);
gtk_main();
return 0;
}
我在点击label
时尝试设置button
的文字。
我试过gtk callback multiple arguments但没有骰子。帮助
答案 0 :(得分:1)
好的,我通过移动data.target = label
来修复它,以便在按钮的g_signal_connect
之后执行。
也许问题产生于在定义对象变量之前使用指针地址。