How I've found to pass data in a callback



Hello everyone,

 

Just thought I'd share a solution to a problem that was bugging me. I'll
bet many of you already know about this, or know of a better way, but
hopefully somebody can use this:

 

In this program I'm writing, I have three buttons that are very similar
in function, they just work on a different set of data. Instead of
creating three different callbacks, I just use the data parameter of the
callback to differentiate between the three buttons. It's probably
easiest to demonstrate with an example:

 

void printFunction (GtkWidget* widget, gpointer data)

{

    printf("The value passed in the callback is: %s\n", (gchar*) data);

}

 

GtkWidget* buttonA = gtk_button_new("Button A");

GtkWidget* buttonB = gtk_button_new("Button B");

GtkWidget* buttonC = gtk_button_new("Button C");

 

g_signal_connect(G_OBJECT(buttonA), "clicked",
G_CALLBACK(printFunction), "buttonA");

g_signal_connect(G_OBJECT(buttonB), "clicked",
G_CALLBACK(printFunction), "buttonB");

g_signal_connect(G_OBJECT(buttonC), "clicked",
G_CALLBACK(printFunction), "buttonC");

 

Hope this helps somebody, and sorry for wasting the other's time.

 

--Matthew




[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]