Question

How do you get the text from a gtk entry widget and then convert that to an integer value. Notice in my code I include a wrapper struct called Window that contains pointers to widgets. In the main, I declare an instance of a Window and then build the correct widgets with the appropriate GTK function calls. I then pass that window object to the function that handles the clicked action. I want to then calculate the numerator divided by the denominator in integer format. Below is my attempt. All the code works except for the button_clicked function. Any ideas?

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

struct Window
{
    GtkWidget *numerator;
    GtkWidget *denominator;
    GtkWidget *button;
    GtkWidget *label;
};


void button_clicked(GtkWidget *widget, gpointer data)
{
    Window* w = (Window*)data;
    char buf[10];

    char buffer[200];

    GtkEntry* e = (GtkEntry*)w->numerator;
    const gchar* entry1 = gtk_entry_get_text(e);

    char* test = (char*)gchar;
    int r = atoi(test);


    sprintf(buf,"%d",r);

    GtkWidget *label = w->label;
    gtk_label_set_text(GTK_LABEL(label), buf);
}


int main(int argc, char*argv[])
{
    GtkWidget *window;
    GtkWidget *table;
    Window w;


    //Set up my window
    gtk_init(&argc,&argv);
    window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
    gtk_window_set_title(GTK_WINDOW(window), "Division");
    gtk_window_set_default_size(GTK_WINDOW(window),500,500);
    gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);

    //Create my table and add it to the window
    table = gtk_table_new(4,2,FALSE);
    gtk_container_add(GTK_CONTAINER(window),table);

    //Create instances of all my widgets
    w.numerator = gtk_entry_new();
    w.denominator = gtk_entry_new();
    w.button = gtk_button_new_with_label("Click");
    w.label = gtk_label_new("result");

    //Attack the widgets to the table
    gtk_table_attach(GTK_TABLE(table), w.numerator,0,1,0,1,GTK_FILL,GTK_FILL,5,5);
    gtk_table_attach(GTK_TABLE(table), w.denominator,0,1,1,2,GTK_FILL,GTK_FILL,5,5);
    gtk_table_attach(GTK_TABLE(table), w.button,0,1,2,3,GTK_FILL,GTK_FILL,5,5);
    gtk_table_attach(GTK_TABLE(table), w.label,0,1,3,4,GTK_FILL,GTK_FILL,5,5);

    //attach the click action to with the button to invoke the button_clicked function
    g_signal_connect(G_OBJECT(w.button),"clicked",G_CALLBACK(button_clicked),&w);   
      g_signal_connect_swapped(G_OBJECT(window),"destroy",G_CALLBACK(gtk_main_quit),NULL);

    gtk_widget_show_all(window);


    gtk_main();

    return 0;
}
Was it helpful?

Solution

If I see this correctly, in your "test code" all you're trying to do is set the label string to the contents of "w->numerator", right?

The line

char* test = (char*)gchar;

looks fishy to me and doesn't even compile, it looks like a typo. Change the "gchar" to "entry1", and it should do what you want it to.

I have a recommendation for you though: use GtkSpinButton instead of GtkEntry. It is like a custom Entry made for numerical values, and the retrieval of such is many times easier.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top