Re: [gtk-list] Passing a class method (not function) usinggtk_signal_connect()




On Sun, 25 Jul 1999, Chun Teck Soon wrote:
> I'm writing in C++ with Gtk lib. My current problem now is that I'm
> trying to send a class method as a callback function using
> gtk_signal_connect. This is done inside a self created class
> 'Application' where the signal connecting action is performed in one of
> the method and connecting to another method of the same class.
>

Class methods and plain functions aren't interchangeable - how can GTK+
(written in C) invoke a method? Methods require a 'this' pointer as an
implicit first argument. So you're just getting a bunch of memory garbage
instead of a 'this' and it's not going to work well. :-)

You can, however, use a static member function. So, I usually do something
like this:

static void static_callback(GtkWidget* widget, gpointer user_data)
{
  MyClass* mc = (MyClass*)user_data;
  mc->callback();
} 

void callback()
{
 // callback stuff here

}

Alternatively, avoid the 'callback()' and do:

static void static_callback(GtkWidget* widget, gpointer user_data)
{
  MyClass* mc = (MyClass*)user_data;
  // access class members via the 'mc' pointer
  mc->tmp = 0;
  mc->foo = "hello";
} 

Alternatively, you could use the Gtk-- wrapper, which has a nice signal
system allowing you to use methods as callbacks.

Havoc





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