GTK+ Forums

Discussion forum for GTK+ and Programming. Ask questions, troubleshoot problems, view and post example code, or express your opinions.
It is currently Thu Oct 23, 2014 1:06 pm

All times are UTC




Post new topic Reply to topic  [ 3 posts ] 
Author Message
 Post subject: [SOLVED] Problem with passing parameter in the callback
PostPosted: Sun Apr 07, 2013 2:19 pm 
Offline

Joined: Thu Apr 04, 2013 7:32 pm
Posts: 2
Hi, I'm new here. I apoligize in advance for my english. It's not my native language.

I just start to learn GTK+ and I have problem with passing parameter in callback function. I was searching on this forum in previous topics for any advice, but I found no solution for my issue. On first sight everything looks fine but... it doesn't work as i expected. Here is the scrap of my code:
Code:
/* callback function */
void button_cf(GtkWidget *button, GdkEvent *event, gpointer data)
{
   gint *s = (gint*) data;
   g_print("%d\n", *s);
}

/* in main funtion */
GtkWidget *button;
gint a = 4;

/*
...
*/

g_signal_connect (button, "clicked", G_CALLBACK (button_cf), (gpointer) &a);

When I click this button, g_print function prints an odd numbers (0 for example) instead of 4. What am I doing wrong?

I would be grateful for any help and for not yelling at me ;)


Last edited by bociu on Sun Apr 07, 2013 4:30 pm, edited 1 time in total.

Top
 Profile  
 
 Post subject: Re: Problem with passing parameter in the callback function
PostPosted: Sun Apr 07, 2013 3:58 pm 
Offline
Never Seen the Sunlight

Joined: Mon Apr 28, 2008 5:52 am
Posts: 764
Location: UK
Hello and welcome,

Your mistake is very common for those starting out. What you have done is used the incorrect arguments for the call-back function. The corrected code is as below and the relevant section is the reference manual is https://developer.gnome.org/gtk3/stable/GtkButton.html#GtkButton-clicked.

All call-back functions will follow a similar pattern, just that the number and type of arguments will vary for each signal.

Code:
/* callback function */
void button_cf(GtkButton *button, gpointer data)
{
   gint *s = (gint*) data;
   g_print("%d\n", *s);
}

/* in main funtion */
GtkWidget *button;
gint a = 4;

/*
...
*/

g_signal_connect (button, "clicked", G_CALLBACK (button_cf), (gpointer) &a);

_________________
E.


Top
 Profile  
 
 Post subject: Re: Problem with passing parameter in the callback function
PostPosted: Sun Apr 07, 2013 4:28 pm 
Offline

Joined: Thu Apr 04, 2013 7:32 pm
Posts: 2
Thanks a lot errol :) The problem is solved. I have to more carefully read documentation.


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

All times are UTC


Who is online

Users browsing this forum: Google [Bot] and 2 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