gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 20:30:46 25 4
gpt4 key购买 nike

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

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);

最佳答案

创建一个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);
...
}

关于c - 如何将多个旋转按钮值传递给函数?(GTK/C),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53440668/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com