Ç - 传递多个参数的回调函数在GTK [英] c - Passing multiple arguments to a callback function in GTK

查看:159
本文介绍了Ç - 传递多个参数的回调函数在GTK的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我想实现如下:用户应能够填写多的点击 gtk_entry 应用之后,就被点击我要的应用按钮发出的信号,是这样的:

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(应用),点击
  G_CALLBACK(apply_clicked),#参数#);

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

之后,在 apply_clicked(),我要保存输入的文本。

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

我的问题是:我如何通过这些 gtk_entry 的到我的回调函数 apply_clicked
如果这是唯一一个我只是将它设置为#参数#,但我应该怎么做多个项目?

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 ?

推荐答案

这样的典型方法是要做到:

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);

,然后在apply_clicked:

and then in apply_clicked:

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

通常context_object将是GtkDialog或任何这些小部件上存在。

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

另外,如果子类GtkDialog,你可以这样做:

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

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

然后,构建你的对话框,只需设置取值范,2,3,等时......你不需要使用g_object_ [G,S] et_data()破解。

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.

这篇关于Ç - 传递多个参数的回调函数在GTK的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆