0

如何在单击单个按钮时更改多个标签值。通常在按钮单击信号连接时,我们只能提供一个用户数据。示例代码

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

我想在单击一次按钮时更改两个标签。

4

1 回答 1

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_dataas userdata 参数并可以访问信号处理程序中的小部件,例如app_data->id_label

于 2013-10-07T11:30:09.350 回答