如何在单击按钮上更改多个标签值。通常在按钮单击信号连接上,我们只能提供一个用户数据。 示例代码
void show_loop(GtkWidget *widget, gpointer user_data)
{
char buf[5];
int no = TxBuf.plBuf[7];
sprintf(buf, "%d",no);
gtk_label_set_text(GTK_LABEL(user_data), buf);
}
ID_label=GTK_WIDGET (gtk_builder_get_object (builder, "label24"));
DLC_label=GTK_WIDGET (gtk_builder_get_object (builder, "label25"));
check = GTK_WIDGET (gtk_builder_get_object (builder, "button3"));
g_signal_connect (check, "clicked", G_CALLBACK (show_loop), DLC_label);
我想在单击按钮上更改两个标签。
答案 0 :(得分:3)
通常,您将定义,分配和填充包含可能必须在初始化代码中动态更改的所有小部件的结构:
typedef struct AppData {
GtkWidget *id_label;
GtkWidget *dlc_label;
} AppData;
...
AppData *app_data = g_new0 (AppData, 1);
app_data->id_label = GTK_WIDGET (gtk_builder_get_object (builder, "label24"));
app_data->dlc_label = GTK_WIDGET (gtk_builder_get_object (builder, "label25"));
您使用app_data
作为userdata参数,并且可以访问信号处理程序内部的小部件,例如: app_data->id_label
。