On Tue, Jul 01, 2014 at 01:39:03AM -0700, Anoop Neem wrote:
> 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)

This basically means that the pointer that you passed to
gtk_entry_get_text was not pointing to a GtkEntry.

> ---------------------------------------------------------------------
> #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;

At this point textEntry is still not initialized, so all this did was
copy the uninitialized value from textEntry to p->one.

>       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();

And here you assign textEntry with the pointer to your new GtkEntry, but
p->one is not updated and sill contains the old value which is used in
calculate.

>       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??

In a large application sure, it makes sense to bundle related things
together as a struct and passit around; but that's not really necessary
in a small application like this. Since you're using Gtk you might just
as well want to check out GObject which formalizes a lot of this stuff.

                Marcus
_______________________________________________
gtk-app-devel-list mailing list
gtk-app-devel-list@gnome.org
https://mail.gnome.org/mailman/listinfo/gtk-app-devel-list

Reply via email to