Glade3 and C programming

牧云@^-^@ 提交于 2019-12-22 12:37:29

问题


I am trying to create a simple app that accepts two integer and displays Sum of two on clicking "Sum" button

And I am new to Glade3, so you can expect blunders

/*
 * Compile me with:
 *   gcc -o test test.c $(pkg-config --cflags --libs gtk+-2.0 gmodule-2.0)
 */

#include <gtk/gtk.h>

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

GtkBuilder *builder;

GtkWidget  *window;

GError     *error = NULL;

void on_button1_clicked(GtkButton *button1, GtkEntry *entry1, GtkEntry *entry2, GtkEntry *entry3 )
{

    const char *input1 = (const char *)malloc(20);
    const char *input2 = (const char *)malloc(20);
    char *result = (char *)malloc(20);
    int input1_int, input2_int, result_int;

    g_print ("Check point 1\n"); //to help debugging
    input1 = gtk_entry_get_text(entry1); //fetching user data from entry1
    input2 = gtk_entry_get_text(entry2); //fetching user data from entry2
    g_print ("Check point 2\n"); //to help debugging

    input1_int = atoi(input1); // String to Integer Conversion
    input2_int = atoi(input2);

    result_int = input1_int + input2_int; //Sum operation

    sprintf(result, "%d", result_int);

    gtk_entry_set_text(entry3, result); //Pushing result of Sum operation into Entry3
}

void on_window1_destroy (GtkObject *object, gpointer user_data)
{
    gtk_main_quit();
}

int main( int argc, char **argv )`
{

    /* Init GTK+ */
    gtk_init( &argc, &argv );

    /* Create new GtkBuilder object */
    builder = gtk_builder_new();

    /* Load UI from file.
     * Replace "sum.glade" with your saved project. */
    if( ! gtk_builder_add_from_file( builder, "sum.glade", &error ) )
    {
        g_warning( "%s", error->message );
        g_free( error );
        return( 1 );
    }

    /* Get main window pointer from UI */
    window = GTK_WIDGET( gtk_builder_get_object( builder, "window1" ) );

    /* Connect signals */
    gtk_builder_connect_signals( builder, NULL );

    /* Destroy builder, since we don't need it anymore */
    g_object_unref( G_OBJECT( builder ) );

    /* Show window. All other widgets are automatically shown by GtkBuilder */
    gtk_widget_show( window );

    /* Start main loop */
    gtk_main();

    return( 0 );

}

Output: Check point 1

(test:10082): Gtk-CRITICAL **: gtk_entry_get_text: assertion `GTK_IS_ENTRY (entry)' failed Segmentation fault (core dumped)


回答1:


you are passing 'NULL' for the callback functions here:

gtk_builder_connect_signals( builder, NULL );

Also remember to free memory allocated to *input1, *input2 and *result.




回答2:


The prototype for the button's clicked signal is wrong. It should be (Reference):

void user_function (GtkButton *button, gpointer user_data)

Probably the best option would be to pass builder into the callback and retrieve the entry widgets from there (unref it after gtk_main) or pass a structure with both widgets.



来源:https://stackoverflow.com/questions/16263216/glade3-and-c-programming

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