简体   繁体   中英

Passing a struct of Gtk Widgets to a callback function

I'm creating a GTK+ application in C,where i need to destroy a widget (for ex: a button) from a callback function.When i call "gtk_widget_destroy" from the function,the widget doesn't get destroyed and i see the following warnings:

(gtkTest:9150): GLib-GObject-WARNING **: invalid uninstantiatable type 'GInterface' in cast to 'GtkObject'

(gtkTest:9150): Gtk-CRITICAL **: IA__gtk_widget_destroy: assertion 'GTK_IS_WIDGET (widget)' failed

how do i access the button widget from inside a callback function ? The only i option i see it is to make this widget global.Any help will be greatly appreciated.

The code snippet is as follows:

typedef struct {
    GtkWidget *button;
} buttonInfo;


/*call back function */
static gpointer _callBackFunc (buttonInfo *buttonTable)
{
    /*do some stuff*/
    gtk_widget_destroy(buttonTable->button);
    //
    return(NULL);
}

/*main*/
int main( int argc, char *argv[])
{

    GtkWidget *testButton;

    buttonInfo *buttonPTR;
    buttonPTR = g_new(buttonInfo,1);
    testButton = gtk_button_new_with_label("Click Me");
    buttonPTR->button = (GtkWidget *) testButton;

    g_signal_connect(G_OBJECT(testButton),"activate",
                     G_CALLBACK(_callBackFunc),buttonPTR);
    /*This works */
    //gtk_destroy_widget(buttonPTR->button);
} 

Your callback function signature must match the signature you see in the signal documentation . In the case of "activate", it should be

static void _callBackFunc (GtkButton *button, gpointer user_data)
{
    buttonInfo *button_info = (buttonInfo *)user_data;

    // ...
}

That said, while you are checking the signature do read the rest of activate-signal documentation: it's probably not the signal you want to use (see "clicked").

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM