2 つの整数を受け入れ、「合計」ボタンをクリックすると 2 の合計を表示する単純なアプリを作成しようとしています。
そして、私は Glade3 を初めて使用するので、大失敗が予想されます
/*
* 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 );
}
出力: チェックポイント 1
(test:10082): Gtk-CRITICAL **: gtk_entry_get_text: assertion `GTK_IS_ENTRY (entry)' failed Segmentation fault (core dumped)