开发者

Get text from a GtkEntry

I'm getting a segmentation fault trying to compile the following code. My question is, Is this the correct way to get text from a GtkEntry?. If it is, Why am I getting the segmentation fault?. If it´s not, What´s the correct way to retrieve text from a GtkEntry?.

void dialogoIngresarDados(GtkWidget *window){
GtkWidget *dialog;
GtkWidget *vbox, *button;
GtkWidget *hBoxDado1, *hBoxDado2, *label1, *label2;
struct textEntries dados;

dialog = gtk_window_new(GTK_WINDOW_TOPLEVEL);
gtk_container_set_border_width(GTK_CONTAINER(dialog),5);
gtk_widget_set_size_request(dialog ,200, 100);
gtk_window_set_title(GTK_WINDOW(dialog), "Dados");
gtk_window_set_resizable(GTK_WINDOW(dialog), FALSE);

vbox = gtk_vbox_new(FALSE, 0);

label1 = gtk_label_new("Dado1");
label2 = gtk_label_new("Dado2");
button = gtk_button_new_from_stock(GTK_STOCK_APPLY);

dados.entryDado开发者_运维百科1 = gtk_entry_new_with_max_length(10);
dados.entryDado2 = gtk_entry_new_with_max_length(10);

hBoxDado1 = gtk_hbox_new(TRUE,0);
hBoxDado2 = gtk_hbox_new(TRUE,0);

gtk_box_pack_start_defaults (GTK_BOX (vbox), hBoxDado1);
gtk_box_pack_start_defaults (GTK_BOX (vbox), hBoxDado2);
gtk_box_pack_start_defaults (GTK_BOX (vbox), button);

gtk_box_pack_start_defaults (GTK_BOX (hBoxDado1), label1);
gtk_box_pack_start_defaults (GTK_BOX (hBoxDado1), dados.entryDado1);

gtk_box_pack_start_defaults (GTK_BOX (hBoxDado2), label2);
gtk_box_pack_start_defaults (GTK_BOX (hBoxDado2), dados.entryDado2);

gtk_container_add (GTK_CONTAINER(dialog), vbox);

g_signal_connect(G_OBJECT(button), "clicked", G_CALLBACK(applyIngresarDados), &dados);
g_signal_connect(G_OBJECT(dialog), "destroy", G_CALLBACK(gtk_main_quit), NULL);

gtk_widget_show_all (dialog);
}

Clicked signal callback.

void applyIngresarDados(GtkButton *button, struct textEntries *dados){
const gchar *dado1;
const gchar *dado2;

dado1 = gtk_entry_get_text(GTK_ENTRY(dados->entryDado1));
dado2 = gtk_entry_get_text(GTK_ENTRY(dados->entryDado2));
}

Structure containing the text entries.

struct textEntries{
GtkWidget *entryDado1;
GtkWidget *entryDado2;
};


In this line:

g_signal_connect(G_OBJECT(button), "clicked", G_CALLBACK(applyIngresarDados), &dados);

you pass a pointer to a dados structures, which is stack-allocated:

struct textEntries dados;

This is wrong, because when the structure is used in a callback, the stack frame containing this structure is destroyed and is probably overwritten by some other data. You're trying to access garbage data in a callback handler.


You callback signature should be like this

gboolean appCallBack(GtkWidget*,gpointer);

You can typecast the gpointer to the structure of your type. Please create a entry using API gtk_entry_new(). I have not found any reference in gtk+2.0 documentation for gtk_entry_new_with_max_length.

GtkWidget *entry;
entry = gtk_entry_new();

Now in your appCallback the function can get the entry text as follows.

GtkWidget *entry = (GtkWidget *) callback_data; //data passed in signal connect.

Then you can get the text from this entry with the call

gchar *text;
text = gtk_entry_get_text(GTK_ENTRY(entry));

Also do the proper error checks.

Or you can declare the textentries structure as a pointer.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜