g_signal_connect_swapped()做了什么? [英] What does g_signal_connect_swapped() do?

查看:170
本文介绍了g_signal_connect_swapped()做了什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据GObject的参考文献


g_signal_connect_swapped(instance,detailed_signal,c_handler,data);将GCallback函数连接到特定对象的信号。


我不明白这是什么意思。这是否意味着 data 将指向由 instance instance 将指向由 data 指向的对象,还是我在这里犯了错误?



如果前者是这种情况,那么这背后的逻辑是什么?解析方案

您理解正确。



这允许你做如下的技巧:你有一个按钮(让我们称之为按钮),这应该隐藏另一个当按下时,我们称之为 textview )。

然后你可以做

  g_signal_connect_swapped(button,'clicked',G_CALLBACK(gtk_widget_hide),textview); 

来实现。当按下按钮时,它会生成'clicked'信号,并且使用 textview 作为第一个参数调用回调,并且按钮作为第二。在这种情况下,回调是 gtk_widget_hide(),它只接受一个参数,所以第二个参数被忽略,因为这就是C调用约定的作用。 b
$ b

与以下内容相同,但更短。

  static void 
on_button_clicked(GtkButton *按钮,GtkWidget * textview)
{
gtk_widget_hide(textview);


...其他...

g_signal_connect(button,'clicked',G_CALLBACK(on_button_clicked),textview);

如果您手动编写界面,基本上可以避免编写额外的函数。当然,可能有一些更实际的用途,我从来没有明白。


According to GObject reference

g_signal_connect_swapped(instance, detailed_signal, c_handler, data); connects a GCallback function to a signal for a particular object. The instance on which the signal is emitted and data will be swapped when calling the handler.

I don't quite get what this means. Does this mean that the data will point to the object pointed to byinstance and instance will point to the object that was pointed to by data or am I making a mistake here?

If former is the case then what is the logic behind this?

解决方案

You understand correctly.

This allows you to do tricks like the following: You have a button (let's call it button), that is supposed to hide another widget (let's call it textview) when pressed.

You can then do

g_signal_connect_swapped(button, 'clicked', G_CALLBACK(gtk_widget_hide), textview);

to achieve that. When the button is pressed, it generates the 'clicked' signal, and the callback is called with textview as the first argument, and button as the second. In this case the callback is gtk_widget_hide() which only takes one argument, so the second argument is ignored, because that's the way the C calling convention works.

It's the same as the following, but shorter.

static void
on_button_clicked(GtkButton *button, GtkWidget *textview)
{
    gtk_widget_hide(textview);
}

...elsewhere...

    g_signal_connect(button, 'clicked', G_CALLBACK(on_button_clicked), textview);

Basically it saves you from having to write an extra function if you hand-code your interface. Of course, there may be some far more practical use that I've never understood.

这篇关于g_signal_connect_swapped()做了什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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