0

I need to get text from a bunch of GtkEntries when a button click. I create a custom struct and pass it to the button's click callback. I don't want to use gtk_entry_get_text(*entry) since I need to pass the struct of GtkEntries.

typedef struct{ const gchar* id1; const gchar* id2; } EntryData; static void on_click(GtkWidget *widget, gpointer data) { EntryData* d= (EntryData*)data; printf ("Entry contents: %s\n", d->id1); printf ("Entry contents: %s\n", d->id2); } int main(int argc,char *argv[]) { // .... GtkButton *button_create_hp; GtkEntry *entry_id1; GtkEntry *entry_id2; gtk_init(&argc, &argv); //...... widget and object initialization gtk_entry_set_text(entry_ssd,""); gchar *strval1="sl"; gchar *strval2="sl"; g_object_get(G_OBJECT (entry_id1), "text", &strval1,NULL); g_object_get(G_OBJECT (entry_id2), "text", &strval2,NULL); EntryData entryData={ .id1= strval1, .id2= strval2 }; g_signal_connect (button_create_hp, "clicked", G_CALLBACK(on_click),&entryData); gtk_main(); return 0; } 

I also tried with g_object_get_property (G_OBJECT (entry_id), "text", &val);

In both cases changed values not printed when the button clicked. Can you suggest a proper way to get values and pass it from GtkEntries

10
  • Why WIData* d=? Shouldn't it be EntryData* d=? Commented Apr 12, 2019 at 13:23
  • Sorry, it's a mistake. corrected @KeineLust Commented Apr 12, 2019 at 13:25
  • This code seems correct to me, take a look in deeper in the //...... widget and object initialization part Commented Apr 12, 2019 at 13:26
  • Well, there is a typo in gchar *strval1="sl"; (is redeclared) but it will not compile in this case. Commented Apr 12, 2019 at 13:27
  • Yep. I renamed some variable to make that clear. In original no compilation or syntax error. @KeineLust Commented Apr 12, 2019 at 13:36

1 Answer 1

1

It's not very obvious from the docs, but when you somehow modify text it may be moved to another location, making previous pointer invalid.

If you don't want to pass GtkEntries to your structures, you can update pointers when EntryBuffer emits "deleted-text" or "inserted-text" (or connect to GtkEntry's "notify::text")

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.