4

So, I'm trying to achieve the following: The user shall be able to fill out multiple gtk_entry's and click Apply after that, on being clicked I want the Apply button to emit a signal, something like this:

g_signal_connect (G_OBJECT (Apply), "clicked", G_CALLBACK(apply_clicked), # an argument #);

Afterwards, in apply_clicked(), I want the entered text to be saved.

My question is: How do I pass those gtk_entry's to my callback function apply_clicked? If it were only one I'd just set it as # an argument #, but what should I do with multiple entries ?

2 Answers 2

5

The typical way of doing this is to do:

g_object_set_data (context_object, "entry1", entry1); g_object_set_data (context_object, "entry2", entry2); g_signal_connect (G_OBJECT (Apply), "clicked", G_CALLBACK (apply_clicked), context_object); 

and then in apply_clicked:

GtkEntry *entry1 = g_object_get_data (context_object, "entry1"); ... 

Usually the context_object will be the GtkDialog or whatever these widgets exist on.

Alternatively, if you subclass the GtkDialog, you can do:

struct _MyDialog { GtkDialog parent_object; GtkEntry *entry1; GtkEntry *entry2; ... }; 

Then, when constructing your dialog, just set entry1, 2, 3, etc... and you don't need to use the g_object_[g,s]et_data() hack.

Sign up to request clarification or add additional context in comments.

2 Comments

Okay, so I'm using your first approach, like you pointed it out, now I'd like to retrieve the text: gchar *string = gtk_entry_get_text(GTK_ENTRY(entry1));, but it won't work - assignment discards ‘const’ qualifier from pointer target type - why is that?
gtk_entry_get_text() returns const char *, not char *. Change your code to const gchar *string = gtk_entry_get_text(GTK_ENTRY(entry1)); and it should compile.
1

create a data structure (a linked list perhaps) to contain pointers to the gtk_entrys and pass that instead. Or better yet, why not just pass a pointer to the object which contains all of thise gtk_entrys?

1 Comment

the problem with this approach is memory management, because now you have to store that list somewhere where you can delete it when it is no longer needed. You could stick it on the GtkDialog, but then why not just use my alternative suggestion instead? :)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.