关于gtk+的两个问题

1、 g_signal_connect and g_signal_connect_swap

 函数形式:

g_signal_connect( gpointer *object,
                  const gchar *name,
                  GCallback func,
                  gpointer userdata );
g_signal_connect_swapped( gpointer *object,
                          const gchar *name,
                          GCallback func,
                          gpointer *slot_object );

  The swapped function allows you to swap the order of the arguments that are passed to the callback function.

 (g_signal_connect_swapped允许你交换传递给回调函数的参数的次序.)

   For example, if you have a GtkButton which will destroy the GtkWindow you would attach a callback function to the "clicked" signal. The callback for the "clicked" callback function is defined in the manual as: 

void (GtkButton *button,
      gpointer   user_data);

  This tells us that when the GtkButton is clicked, the callback function we connect will recieve the GtkButton object first, and our user_data second. This is what would happen if we used g_signal_connect ().

  However, if we want to attach a callback function in which the first argument is the user_data, then we use g_signal_connect_swapped (). This allows us to use some already written functions as callbacks such as gtk_widget_destroy (). 

Example 1: Without using swapped

void my_destroy_callback (GtkButton *button, gpointer user_data)
{
    /* destroy the window */
    gtk_widget_destroy (GTK_WIDGET (user_data));
}
int main()
{
    GtkWidget *button;
    GtkWidget *window;
    /* create and pack widgets here ... */
    g_signal_connect (G_OBJECT (button), "clicked", 
                      G_CALLBACK (my_destroy_callback), (gpointer)window);
    gtk_main();
    return 0;
}
Example 2: Swapping Arguments
int main()
{
    GtkWidget *button;
    GtkWidget *window;
    /* create and pack widgets here ... */
    g_signal_connect_swapped (G_OBJECT (button), "clicked", 
                              G_CALLBACK (gtk_widget_destroy), (gpointer)window);                     
    gtk_main();
    return 0;
}

2、delete_event and destroy


The difference between "delete_event" and "destroy" is, as far as i understood from reading the official documentation, is that the "delete_event" is an EVENT, and the "destroy" is a SIGNAL. 
Events are issued from the Operating System (more precisely, from the Window engine), and Signals are issued from the widgets themselves. So when a window is being closed (the user pressed the "x" button or the system asks for closing the program), the Operating System issues the "delete_event". Then, if the process of this event is not handled by a callback, or the callback that handles the event returns false (0), then a "destroy" signal is issued for the window. 
Typical pattern: The user attempts to close the window. The system asks the user "Are you sure you want to quit?" Y / N   
In this case, you can handle the situation by implementing the callback function connected to the "delete_event", in which you will ask the question. If the user says Yes, you return false: the "destroy" signal will be issued and then the window closes. If the user says No, you return true: the "destroy" signal will not be issued, and the window won't close. 
You may (should) also implement a callback attached to the "destroy" signal, in which you call for the gtk_main_quit() function in order to quit the gtk event loop properly.

    

    大意:delete_event是一个EVENT,destroy是一个SIGNAL。EVENT发自操作系统,SIGNAL则产生于组件本身。当你关闭一个窗口时,操作系统发出一个delete_event,如果确认关闭(Y),则会发出destroy信号。

更多信息可点击:http://developer.gnome.org/gobject/stable/gobject-Signals.html#g-signal-connect

                            http://www.gtkforums.com/viewtopic.php?t=1177



转载于:https://my.oschina.net/aram/blog/96233

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值