I'm trying to learn how to make GUIs using gtk+ 3.0. I want to pass a simple argument, an integer, to a callback function, so that when I press the button the value of the argument changes. Here's my code:
#include <gtk/gtk.h>
void buttonFunction(GtkWidget * widget, gpointer data, int & n){
n = 1;
}
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);
g_signal_connect(G_OBJECT(button), "clicked", G_CALLBACK(buttonFunction), n);
gtk_main();
return 0;
}
The only way I found to pass the argument was as a pointer:
void buttonFunction(GtkWidget * widget, gpointer * data);
g_signal_connect(G_OBJECT(button), "clicked", G_CALLBACK(buttonFunction), &n);
How do I pass multiple arguments this way tho?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…