[英]how to create array of button in gtk+
I want to create array of buttons in gtk+. 我想在gtk +中创建按钮数组。 So, how to modify 'gtkWidget *button' to create array of button.
因此,如何修改“ gtkWidget * button”以创建按钮数组。 I have modified the declaration to 'gtkWidget (*button)[20]' and used this button in below code.
我已将声明修改为'gtkWidget(* button)[20]',并在下面的代码中使用了此按钮。 But, it's showing an error.
但是,它显示了一个错误。 " incompatible types when assigning to type 'struct GtkWidget *[20]' from type 'struct GtkWidget * '"
“从类型'struct GtkWidget *'分配给'struct GtkWidget * [20]'类型时,类型不兼容”
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. 它在内部是无效的赋值,就像
'struct GtkWidget *[20]'
表示数组下标指针。
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. 首先,在Gtk3中不推荐使用GtkTable,因此应改用GtkGrid。
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
. 在
ip_button_callback()
函数中,可以使用gtk_button_get_label()
获取与按钮关联的IP地址,而在status_button_callback()
,可以获取作为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). 一个非常优雅的解决方案可能是使用IP地址作为表键将这些按钮存储在GHashTable中,但这需要在设计上进行更多思考(并且需要更多背景信息)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.