如何将多个旋转按钮值传递给函数?(GTK / C)

时间:2018-11-23 04:30:29

标签: c gtk3

我无法通过旋转按钮传递多个值 我尝试使用按钮进行许多回调,但是我需要同时接收和操作许多变量。

gint grab_int_value (GtkSpinButton *spinbutton, gpointer user_data)
{
    gint steps = gtk_spin_button_get_value_as_int (user_data);
    printf ("The value of the step is: %d ms.\n", steps);
}

done = gtk_button_new_with_mnemonic("_DONE");
g_signal_connect(done, "clicked", G_CALLBACK(grab_int_value), 
                                               spin_button1);
g_signal_connect(done, "clicked", G_CALLBACK(grab_int_value), 
                                               spin_button2);

1 个答案:

答案 0 :(得分:0)

创建一个struct的指针,该指针指向您需要在回调中处理的所有对象,并添加指向该对象的指针作为用户数据:

typedef struct {
    GtkWidget *spin_button1;
    GtkWidget *spin_button2;
} Widgets;

...

Widgets* w = g_new(Widgets, 1);
w->spin_button1 = spin_button1;
w->spin_button2 = spin_button2;

...

g_signal_connect(done, "clicked", G_CALLBACK(grab_int_value), w);
g_signal_connect(done, "clicked", G_CALLBACK(grab_int_value), w);

...

int grab_int_value(GtkSpinButton *spinbutton, gpointer user_data) {
    Widgets *w = (Widgets *) user_data;
    int steps = gtk_spin_button_get_value_as_int(w->spin_button1);
    ...
}