Re: multiple data args to gtk_signal_connect



> hm... yes i thought of that, too, but i would expect that a function
> which is as basic as gtk_signal_connect should provide some way of
> passing multiple args. i have this problem quite often and defining a
> struct every time doesn't seem the most elegant way... however, i'll
> make use of your suggestion as i can't think for a better solution for
> myself. by the way, is it possible creating and passing a struct on the
> fly (in the call of gtk_signal_func)? it wouldn't probably look that
> nice but saves lines ;)
> 
> at least it seems my question wasn't that silly =)

Depending on scope and other things, you could also do
gtk_object_set_data() either to an arbitrary object that you create and
pass as the 4th arg or to an already existing widget that you want to
pass. In this particular instance, you would probably want to create a
GtkObject and pass it with those strings as data on it.

GtkObject *passme;

passme = gtk_object_new(... [I don't remember the args offhand]);
gtk_object_set_data(passme, "str1", str1);
gtk_object_set_data(passme, "str2", str2);
gtk_signal_connect(GTK_OBJECT(some_button), "clicked",
                   GTK_SIGNAL_FUNC(my_func), passme);

and in the function my_func:

str1 = gtk_object_get_data(passme, "str1");
str2 = gtk_object_get_data(passme, "str2");

and you get the idea. I did a grep for gtk_object_set_data() to see how
often it was used in other GNOME apps (to be sure it wasn't taboo. :) and
it seems very common. However, if you are going to do one particular case
several times or pass a lot more than two or three data fields, you are
probably best off creating a struct, because it's easier to understand and
follow.

Joe







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