What does g_signal_connect_swapped() do?

浪尽此生 提交于 2019-11-29 09:06:16

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.

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