Lakindu Akash
Lakindu Akash

Reputation: 1036

Get text from GtkEntry with property "text"

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

Upvotes: 0

Views: 205

Answers (1)

Alexander Dmitriev
Alexander Dmitriev

Reputation: 2525

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")

Upvotes: 1

Related Questions