GTK/Glade text entry string to global variable in C -
i creating menu gui game using glade , gtk in c. have text entry box , button. when button pressed, value inside text entry needs saved global variable. don't know how value inside text entry i'm bit stuck. great.
function
void on_okbutton_clicked (gtkbutton *object, gpointer user_data) { //??????? }
edit:
this current code looks like
#include <stdio.h> #include <gtk/gtk.h> #include <python2.7/python.h> void on_window1_destroy (gtkobject *object, gpointer user_data) { gtk_main_quit(); } void on_okbutton_clicked (gtkbutton *object, gpointer user_data) { gchar *entry_value;//this can global variable, too, of course entry_value = gtk_entry_get_text(//get text function gtk_entry(//use gtk_entry widget (gtkwidget *) user_data //cast gtkwidget pointer ) ); } int main(int argc, char *argv[]) { gtkbuilder *gtkbuilder; gtkwidget *window; gtkwidget *quit; gtkwidget *ok; gtkwidget *entry = gtk_entry_new(); gtk_init(&argc, &argv); gtkbuilder = gtk_builder_new(); gtk_builder_add_from_file(gtkbuilder, "ipenter.glade", null); window = gtk_widget(gtk_builder_get_object(gtkbuilder, "window1")); ok = gtk_widget(gtk_builder_get_object(gtkbuilder, "okbutton")); g_signal_connect(g_object(ok),"clicked",g_callback(on_okbutton_clicked),entry); g_object_unref(g_object(gtkbuilder)); gtk_widget_show(window); gtk_main(); return 0; }
assuming connected signal correctly, ought work fine:
gchar *entry_value;//this can global variable, too, of course entry_value = gtk_entry_get_text(//get text function gtk_entry(//use gtk_entry widget (gtkwidget *) user_data //cast gtkwidget pointer ) );
just sure, how should connect signal:
g_signal_connect( g_object(your_btn), "clicked", g_callback( on_okbutton_clicked ), txt_entry_pointer );
where txt_entry_pointer
is, of course, pointer entry widget. not, however, make surprisingly common mistake of passing address of local pointer here. if create entry widget so:
gtkwidget *entry = gtk_entry_new();
and attempt connect signal so:
g_signal_connect( g_object(your_btn), "clicked", g_callback( on_okbutton_clicked ), &entry// <=== wrong!! );
you passing pointer stack pointer, resulting in undefined behaviour. drop ampersand code above.
also worth mentioning gchar
typedef char
type. means that:
gchar *foobar;
is same writing
char *foobar;
as can see here.
Comments
Post a Comment