Gabriel
Gabriel

Reputation: 111

Get value on click

I need to get a value when I click on a text, but it seems that for some reason, the pointer gets lost and returns a random value, I want to do this:

static void click(GtkWidget* widget, long* click){
 cout << "Long value is: " << *click << endl;
}

int main(){
   gtk_init(0,0);
   //......... MORE CODE ......
   GtkWidget* label  = gtk_image_menu_item_new_with_label(text);
   long value = 50509;
   g_signal_connect(label, "button_press_event", G_CALLBACK(click), &value);
   //......... MORE CODE ......
   gtk_main();
}

(C++ 14) can anybody help me?

Upvotes: 1

Views: 89

Answers (1)

Gabriel
Gabriel

Reputation: 111

I found the error, it was something totally simple, when I pass a value in the function, g_signal_connect, it becomes a gpointer, a pointer supports 32bits of data, while the long consumes 64bits, I changed long to int and it worked perfectly!. Solution:

static void click(GtkWidget *widget,GdkEvent  *event, gpointer  data){
 cout << "Int value is: " << GPOINTER_TO_INT(data) << endl;
}

int main(){
   gtk_init(0,0);
   //......... MORE CODE ......
   GtkWidget* label  = gtk_image_menu_item_new_with_label(text);
   int value = 50509;
   g_signal_connect(label, "button_press_event", G_CALLBACK(click), GINT_TO_POINTER(value));
   //......... MORE CODE ......
   gtk_main();
}

Thanks @gerhardh for commenting.

Upvotes: 1

Related Questions