|
|
| Author |
Message |
|
|
guy.murray
Joined: 16 Feb 2008 Posts: 3
|
Posted: Sun Feb 17, 2008 8:34 pm Post subject: gtk+ with c++ classes - connecting signal to class method |
|
|
Hi all,
I have a c++ class (Bodget) which contains a pointer to a gtk button as a private property. The class's constructor function defines the button and adds it to a container (box)...
Bodget::Bodget()
{
//initialise properties
contents = 0;
//Create Button
button = gtk_button_new_with_label("C++Button");
gtk_container_add(GTK_CONTAINER(box), button);
gtk_widget_show(button);
}
This works fine, and the button duly appears. However, when I add the following line to connect the button's clicked event to a public method (incContent) of the same class, it refuses to compile...
gtk_signal_connect(GTK_OBJECT(button), "clicked", GTK_SIGNAL_FUNC(incContents), NULL);
Compiler error message reads...
error: invalid use of member (did you forget the ‘&’ ?)
Can anyone explain why it doesn't work and show me the correct way to do it? (Or tell me that what I am trying to do is just plain sick and wrong!)
Many thanks
Guy |
|
| Back to top |
|
 |
Lindley Familiar Face
Joined: 19 Dec 2007 Posts: 13
|
Posted: Mon Feb 18, 2008 8:46 pm Post subject: |
|
|
The easiest way to do this is to make the handler method a static member, and then pass "this" as the user data pointer.
The problem is that class methods have an extra implicit argument, which is the this pointer. |
|
| Back to top |
|
 |
guy.murray
Joined: 16 Feb 2008 Posts: 3
|
Posted: Wed Feb 20, 2008 12:38 pm Post subject: |
|
|
Thanks, I will try this approach, though I still don't understand why one can't reference an instance method directly.
A similar problem arises if one references an object method external to a gtk_signal_connect function such as say
gtk_signal_connect(GTK_OBJECT(button), "clicked", GTK_SIGNAL_FUNC(myObject->incContents), NULL);
Guy |
|
| Back to top |
|
 |
Lindley Familiar Face
Joined: 19 Dec 2007 Posts: 13
|
Posted: Wed Feb 20, 2008 3:43 pm Post subject: |
|
|
Gtk is a C library. It has no concept of objects. It expects a function which takes certain arguments and returns a given type.
Even if your non-static method appears to take those arguments and return that type, under the hood there's an additional argument: the this pointer for the calling object. Thus, the method doesn't match the signature Gtk expects.
Static functions don't take a this pointer implicitly, so they're fine. And if you pass the this pointer yourself as the user data, you can still do everything you need to. |
|
| Back to top |
|
 |
guy.murray
Joined: 16 Feb 2008 Posts: 3
|
Posted: Thu Feb 21, 2008 2:25 pm Post subject: |
|
|
Many thanks, the mist is beginning to clear.
Guy |
|
| Back to top |
|
 |
dreblen Familiar Face
Joined: 14 Jun 2007 Posts: 38
|
Posted: Tue Mar 11, 2008 12:56 am Post subject: |
|
|
you might also consider trying gtkmm ( http://gtkmm.org ), which is the C++ version of gtk.
I've never used it myself, but I imagine that it's easier to use something that's meant for C++ |
|
| Back to top |
|
 |
|