GTK+ Forums

Discussion forum for GTK+ and Programming. Ask questions, troubleshoot problems, view and post example code, or express your opinions.
It is currently Mon Sep 22, 2014 10:15 pm

All times are UTC




Post new topic Reply to topic  [ 6 posts ] 
Author Message
 Post subject: gtk+ with c++ classes - connecting signal to class method
PostPosted: Sun Feb 17, 2008 8:34 pm 
Offline

Joined: Sat Feb 16, 2008 10:56 pm
Posts: 3
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


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 18, 2008 8:46 pm 
Offline
Familiar Face

Joined: Wed Dec 19, 2007 5:06 pm
Posts: 13
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.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 20, 2008 12:38 pm 
Offline

Joined: Sat Feb 16, 2008 10:56 pm
Posts: 3
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


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 20, 2008 3:43 pm 
Offline
Familiar Face

Joined: Wed Dec 19, 2007 5:06 pm
Posts: 13
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.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Feb 21, 2008 2:25 pm 
Offline

Joined: Sat Feb 16, 2008 10:56 pm
Posts: 3
Many thanks, the mist is beginning to clear.

Guy


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 11, 2008 12:56 am 
Offline
Never Seen the Sunlight

Joined: Thu Jun 14, 2007 11:02 pm
Posts: 923
Location: Falun, WI USA
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++


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 6 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 3 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group