To pass multiple arguments, you define a structure, fill it, and pass a pointer to the structure as the gpointer user_data
parameter of the g_signal_connect
, which is the last parameter. Then in your callback, you just cast the user_data
parameter to a pointer to your structure.
int main (int argc, char **argv)
{
int n = 0;
GtkWidget *window;
GtkWidget *button;
gtk_init (&argc,&argv);
window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
button = gtk_button_new_with_label ("Osss");
gtk_container_add (GTK_CONTAINER(window), button);
gtk_widget_show_all (window);
g_signal_connect(G_OBJECT(window), "destroy", G_CALLBACK(gtk_main_quit), NULL);
/* Here's the magic: you pass a pointer to the variable you'd like to modify
* in the callback, be it a simple variable or a struct */
g_signal_connect(G_OBJECT(button), "clicked", G_CALLBACK(buttonFunction), &n);
gtk_main();
return 0;
}
void on_button_clicked (GtkButton *button, gpointer user_data) /* No extra parameter here ! */
{
/* you cast to the type of what you passed as last argument of g_signal_connect */
int *pn = user_data;
*pn = 1;
}
You MUST use the signature of the callback defined in the documentation (look at the "signals" section of the documentation for GtkButton), you can't make things up. BTW, you can't pass n
as a reference instead of a pointer. If you want to use GTK in C++, give a look at GTKmm.