質問

I want to create array of buttons in gtk+. So, how to modify 'gtkWidget *button' to create array of button. I have modified the declaration to 'gtkWidget (*button)[20]' and used this button in below code. But, it's showing an error. " incompatible types when assigning to type ‘struct GtkWidget *[20]’ from type ‘struct GtkWidget * ’"

gtkWidget (*button)[20];
static char *values[100] = 

 {  "127.0.0.1",   "Idle",
 "192.168.73.129", "Idle", 
 "192.168.73.130", "Idle",
 "192.168.73.131", "Idle",
 "192.168.73.132", "Idle",
 "192.168.73.129", "Idle",
 "192.168.73.131", "Idle", };

for(i=0; i < 6; i++) {
for( j=0; j < 2; j++) {
  button1[pos] = gtk_button_new_with_label(values[pos]);
  gtk_table_attach_defaults(GTK_TABLE(table), button1[pos], j, j+1, i+1, i+2 );
  pos++;
} }
役に立ちましたか?

解決

gtkWidget (*button)[20]; it's invalid assignment internally it's like ‘struct GtkWidget *[20]’ means array subscript pointer.

create array of buttons in simple way like

GtkWidget *buttons[3][3];
  for (i=0;i<3; i++)
    for (j=0;j<3; j++) 
          buttons[i][j] = gtk_button_new ();

他のヒント

First of all, GtkTable is deprecated in Gtk3, so you should use a GtkGrid instead.

Second, you don’t really need to store those widgets in an array, so simply iterating through the values list and create two buttons in each steps is a bit better approach:

GtkWidget *ip_button,
          *status_button;
gchar *values[] = {
    "127.0.0.1",      "Idle",
    "192.168.73.129", "Idle", 
    "192.168.73.130", "Idle",
    "192.168.73.131", "Idle",
    "192.168.73.132", "Idle",
    "192.168.73.129", "Idle",
    "192.168.73.131", "Idle",
};

for (i = 0; i < value_count; i += 2) {
    ip_button = gtk_button_new_with_label(values[i]);
    gtk_grid_attach(grid, button, i, 0, 1, 1);
    g_signal_connect(G_OBJECT(button), "clicked", G_CALLBACK(ip_button_callback), NULL);

    status_button = gtk_button_new_with_label(values[i + 1]);
    gtk_grid_attach(grid, button, i, 1, 1, 1);
    g_signal_connect(G_OBJECT(button), "clicked", G_CALLBACK(status_button_callback), ip_button);
}

In the ip_button_callback() function you can use gtk_button_get_label() to get the IP address associated with the button, while in status_button_callback() you can get the label of the button passed as user_param.

A much elegant solution may be to store these buttons in a GHashTable using the IP addresses as table keys, but that takes a bit more thinking on the design (and needs a lot more background information).

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top