GTK+ Forums Forum Index GTK+ Forums
Discussion forum for GTK+ and Programming. Ask questions, troubleshoot problems, view and post example code, or express your opinions.
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

gtk+ with c++ classes - connecting signal to class method

 
Post new topic   Reply to topic    GTK+ Forums Forum Index -> GTK+ Programming
Author Message
guy.murray



Joined: 16 Feb 2008
Posts: 3

PostPosted: Sun Feb 17, 2008 8:34 pm    Post subject: gtk+ with c++ classes - connecting signal to class method Reply with quote

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

PostPosted: Mon Feb 18, 2008 8:46 pm    Post subject: Reply with quote

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

PostPosted: Wed Feb 20, 2008 12:38 pm    Post subject: Reply with quote

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

PostPosted: Wed Feb 20, 2008 3:43 pm    Post subject: Reply with quote

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

PostPosted: Thu Feb 21, 2008 2:25 pm    Post subject: Reply with quote

Many thanks, the mist is beginning to clear.

Guy
Back to top
dreblen
Never Seen the Sunlight


Joined: 14 Jun 2007
Posts: 534
Location: Falun, WI USA

PostPosted: Tue Mar 11, 2008 12:56 am    Post subject: Reply with quote

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
Display posts from previous:   
Post new topic   Reply to topic    GTK+ Forums Forum Index -> GTK+ Programming All times are GMT
Page 1 of 1

 


Powered by phpBB © 2001, 2005 phpBB Group
CodeBB 1.0 Beta 2
Protected by Anti-Spam ACP