Passing additional arguments to gtk function

后端 未结 3 1266
夕颜
夕颜 2020-12-18 11:46

I\'m trying to learn how to make GUIs using gtk+ 3.0. I want to pass a simple argument, an integer, to a callback function, so that when I press the button the value of the

3条回答
  •  心在旅途
    2020-12-18 12:36

    int a = 42;
    `gpointer ptr = GINT_TO_POINTER (a)`
    //GUINT_TO_POINTER (a), GBOOLEAN_TO_POINTER (a), GSIZE_TO_POINTER (a)
    reverse:
    
    int a2 = GPOINTER_TO_INT (ptr);
    //GPOINTER_TO_UINT (ptr), GPOINTER_TO_...
    
    The above works perfectly fine to pass integer data thru g_signal_connect. Used as follows.
    
    GtkWidget *Parison_ON_Buttons[10];
    
    for (i=0; i < 8; i++)
    {
        gpointer ONptr = GINT_TO_POINTER (i);
        g_signal_connect(GTK_SPIN_BUTTON(Parison_ON_Buttons[i]), "activate", G_CALLBACK 
        (Get_Parison_ON_Values),ONptr);
    }
    
    static gboolean Get_Parison_ON_Values (GtkSpinButton *button, gpointer data)
    {
     int ONButtonNUmPressed = GPOINTER_TO_INT (data);
    ....
    }
    

提交回复
热议问题