g_signal_connect_swapped() 的作用是什么?

发布于 2024-08-19 10:01:35 字数 347 浏览 6 评论 0原文

根据GObject参考

g_signal_connect_swapped(实例、detailed_signal、c_handler、数据);将 GCallback 函数连接到特定对象的信号。发出信号的实例和调用处理程序时将交换数据的实例。

我不太明白这意味着什么。这是否意味着 data 将指向 instance 所指向的对象,而 instance 将指向 instance 所指向的对象>data 或者我在这里犯了一个错误?

如果是前者,那么这背后的逻辑是什么?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

尬尬 2024-08-26 10:01:35

你理解正确。

这允许您执行如下操作:您有一个按钮(我们称之为 button),按下时应该隐藏另一个小部件(我们称之为 textview) 。

然后你就可以做到

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

这一点。当按钮被按下时,它会生成“clicked”信号,并使用 textview 作为第一个参数和 button 作为第二个参数来调用回调。在本例中,回调函数是 gtk_widget_hide(),它只接受一个参数,因此第二个参数被忽略,因为这就是 C 调用约定的工作方式。

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

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

基本上,如果您手动编写界面代码,它可以使您不必编写额外的函数。当然,可能还有一些我从未理解过的更实际的用途。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文