Daniel Jaló
Daniel Jaló

Reputation: 157

How to pass a label as an extra callback function argument

So, here's my code:

#include <gtk/gtk.h>

void buttonCall(GtkWidget * widget1, GtkWidget * widget2){//gpointer data){

gtk_label_set_text(*label,"some other label");

}

int main(int argc, char **argv){

GtkWidget * window;
GtkWidget * frame;
GtkWidget * button;
GtkWidget * label;

gtk_init(&argc,&argv);

window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
frame = gtk_fixed_new();

gtk_container_add(GTK_CONTAINER(window),frame);


button = gtk_button_new_with_label("button");
label = gtk_label_new("some label");

gtk_container_add(GTK_CONTAINER(frame),label);
gtk_fixed_put(GTK_FIXED(frame), label, 10,50);

gtk_container_add(GTK_CONTAINER(frame),button);
gtk_fixed_put(GTK_FIXED(frame), button, 10,100);

g_signal_connect(G_OBJECT(button),"clicked",G_CALLBACK(buttonCall),&label);

gtk_widget_show_all(window);

gtk_main();
return 0;
}

I basically want to click on a button and change the text on my label. I thought that by passing the address of label I'd be able to do that, however it isn't working. Is my implementation incorrect?

Any help will be appreciated! Thanks!

Upvotes: 0

Views: 309

Answers (1)

Jussi Kukkonen
Jussi Kukkonen

Reputation: 14587

That code doesn't even compile, right? That typically means the implementation is not correct. You are now making us do the debugging for you: if you don't understand the compile errors, at least paste them in the question. If something is "not working", you need to say exactly what is failing.

Now, label is already a pointer in main() so you don't need to get the address for g_signal_connect(): just remove the '&' from '&label'. In the clicked handler your second argument is called widget2 so that's what you need to use inside the function:

gtk_label_set_text(GTK_LABEL(widget2),"some other label");

That should make the code work, but I'll give another piece of advice: using GtkFixed is a bad idea in almost every case: Learn to do box layouts with GtkGrid (if your GTK version has it) or GtkBoxes, it will pay out in the end.

Upvotes: 1

Related Questions