Segmentation fault in creating basic app using GTK+ (with C)

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



This application is intended to convert value entered in Text entry
field (in Celcius) to Farenheit in label. I was having hard time in
altering the properties of two widgets (other than calling widget) in
the same callback, hence created a structure 'struct mulptr' for passing
two widgets. But when i click on the button application closes giving me
some error:


xxx@ubuntu:~/gtk$ gcc `pkg-config --cflags gtk+-3.0` -o 4 4.c
`pkg-config --libs gtk+-3.0`
xxx@ubuntu:~/gtk$ ./4

(4:4221): Gtk-CRITICAL **: gtk_entry_get_text: assertion 'GTK_IS_ENTRY
(entry)' failed
Segmentation fault (core dumped)


------------------------------
---------------------------------------
#include<gtk/gtk.h>
#include<string.h>
#include<stdlib.h>

char a[15] = "Result here";

struct mulptr
{
        GtkWidget *one;
        GtkWidget *two;
};

int tofarenheit(int c)
{
        return (c*1.8 + 32);
}

void calculate(GtkWidget *widget, gpointer data)
{
        GtkWidget *textEntry = ((struct mulptr *)data)->one;
        GtkWidget *label = ((struct mulptr *)data)->two;
        int res = 0, F;

        res = atoi(gtk_entry_get_text(GTK_ENTRY(textEntry)));
        if(res)
        {
                F = tofarenheit(res);
                sprintf(a, "%d", F);
        }
        else
                strcpy(a, "Invalid Input!");
        gtk_label_set_text(GTK_LABEL(label), a);
}

int main(int argc, char *argv[])
{
        //Decleration
        GtkWidget *window;
        GtkWidget *calButton;
        GtkWidget *textEntry;
        GtkWidget *label;
        GtkWidget *grid;
        struct mulptr *p = (struct mulptr *)malloc(sizeof(struct mulptr));
        p->_one_ = textEntry;
        p->two = label;

        //Initilization
        gtk_init(&argc, &argv);

        window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
        //gtk_window_set_default_size(GTK_WINDOW(window), 480, 320);
        gtk_window_set_title(GTK_WINDOW(window), "Celcius to Ferenheit
Converter");
        gtk_container_set_border_width(GTK_CONTAINER(window), 10);
        grid = gtk_grid_new();

        label = gtk_label_new(a);
        textEntry = gtk_entry_new();
        calButton = gtk_button_new_with_label("Calculate");

        //Adding widget to frame
        gtk_container_add(GTK_CONTAINER(window), grid);
        gtk_grid_attach(GTK_GRID(grid), textEntry, 0, 0, 1, 1);
        gtk_grid_attach(GTK_GRID(grid), label, 1, 0, 1, 1);
        gtk_grid_attach(GTK_GRID(grid), calButton, 0, 1, 1, 1);


        //Signal Handlers
        g_signal_connect (window, "delete-event", G_CALLBACK (gtk_main_quit),
NULL);
        g_signal_connect (calButton, "clicked", G_CALLBACK(calculate), p);


        //Essentials
        gtk_widget_show_all(window);
        gtk_main();
        return 0;
}
---------------------------------------------------------------------

Please help me with this piece of code?? Is it all right to use this
kind of struct??
_______________________________________________
gtk-list mailing list
gtk-list@xxxxxxxxx
https://mail.gnome.org/mailman/listinfo/gtk-list

[Index of Archives]     [Touch Screen Library]     [GIMP Users]     [Gnome]     [KDE]     [Yosemite News]     [Steve's Art]

  Powered by Linux