c - Passing multiple arguments to a callback function in GTK

吃可爱长大的小学妹 提交于 2020-01-14 13:07:04

问题


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 ?


回答1:


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.




回答2:


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?



来源:https://stackoverflow.com/questions/6684466/c-passing-multiple-arguments-to-a-callback-function-in-gtk

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!