我正在制作这样的电路板
GtkWidget *board[x][y];
如果我按下一系列按钮,我怎么知道按下了哪个按钮?
g_signal_connect(G_OBJECT(board[][]), "clicked",
G_CALLBACK(board_button_pressed), NULL);
// I want to know what [][] they pressed, how could I verify/check this?
返回按下阵列的哪个按钮?或者我是否必须为每个板块制作单独的功能?
例如:
OOO
OXO
OOO
如果所有按钮的名称相同,如何知道按下了哪个按钮?
答案 0 :(得分:2)
最简单的方法之一是在将回调连接为数据时发送信息。这些方面的东西:
...
typedef struct _identifier{
int x;
int y;
}identifier;
static void button_clicked_cb(GtkButton *button, gpointer data)
{
(void)button; /*To get rid of compiler warning*/
identifier *id = data;
printf("\n id = %d, %d\n", id->x, id->y);
return;
}
....
identifier id[x*y]; /* Size of x*y of the board*/
unsigned int counter = 0;
for (i = 0; i < x; i++)
{
for (j = 0; j < y; j++)
{
id[counter].x = i;
id[counter].y = j;
board[i][j] = gtk_button_new ();
g_signal_connect(board[i][j], "clicked", G_CALLBACK(button_clicked_cb), &id[counter]);
counter++;
}
}
请注意,"clicked"
信号仅与GtkButton
相关联。如果您需要使用GtkWidget
,请查看"button-press-event"
或"button-release-event"
,在这种情况下,回调签名也会更改。
希望这有帮助!