0

I would like to update a text entry on keypress event.

My scenario doesn't have a keyboard, so I am generating it programmatically and trying to update text entry from keypress event handler.

On each key press, I need to update the text entry in GTK window. How can I achieve this? I tried

gtk_entry_set_text (GTK_ENTRY (entry1), buffer);

but it gives a segmentation fault.

I will be getting data from some other user threads, I need to update the text entry with new data and show the gtk window with updated text entry.

The following is my complete code:

GtkEntry *entry1;

static gboolean kp_event_S1_1(GtkWidget *widget, GdkEventExpose *event, gpointer data)
{       
    printf(" - kp_event_S1_1 - \n");
    gtk_entry_set_text (GTK_ENTRY (entry1), buffer);
}


void S1_1(void )
{   
    GtkWidget *Win_1;
    GtkBuilder *builder;        
    builder = gtk_builder_new ();
    gtk_builder_add_from_file (builder, "/home/glade/glade1.glade", NULL);
    window = GTK_WIDGET (gtk_builder_get_object (builder, "Win_1"));
    g_signal_connect_swapped(G_OBJECT(window), "destroy", G_CALLBACK(gtk_main_quit),NULL );
    g_signal_connect(G_OBJECT (window), "key_press_event", G_CALLBACK (kp_event_S1_1), NULL);
    gtk_widget_show_all(window);    
    gtk_main();
}
user12205
  • 2,684
  • 1
  • 20
  • 40
tux tux
  • 25
  • 1
  • 5
  • 1
    `gtk_entry_set_text` is the correct API. Getting a segmentation fault means `entry1` is not a `GtkEntry *` instance or `buffer` is not a NUL-terminated string: it is that simple. – ntd May 29 '15 at 09:45
  • question modified with code. buffer is filled from another thread – tux tux May 29 '15 at 11:18
  • @tuxtux Where is `buffer`? How is it declared? You're still not showing it. And being updated from a different thread sounds super-scary. Try with a constant (a string literal) instead of `buffer` in the call, see if that works. – unwind May 29 '15 at 11:25
  • What you posted does not show the problem: You should make a minimal self contained test case. – Jussi Kukkonen May 29 '15 at 11:57
  • also tried "ok" sample string instead of buffer(to avoid external thread doubts).but result is same. – tux tux May 29 '15 at 12:14
  • Your handler uses GdkEventExpose, not GdkEventKey. It also doesn't show what creates the GtkEntry. Can you add the code you replaced with `//create window`? – andlabs May 29 '15 at 13:55
  • added code for the //create window. Actually i am creating window from glade file using builder option – tux tux May 30 '15 at 07:06
  • Hi all, today got some solution , i used custom user signal , that worked fine . g_signal_new("user-signal", G_TYPE_OBJECT, G_SIGNAL_RUN_FIRST, 0, NULL, NULL, g_cclosure_marshal_VOID__POINTER, G_TYPE_NONE, 1, G_TYPE_POINTER); same keyevent handler i redirected to user signal. – tux tux May 30 '15 at 07:09
  • Don't create custom signals. Your problem is that you never assigned `entry1` from your builder file! – andlabs May 30 '15 at 07:35
  • thanks andlab. jst now added the assignment,its able to access now. – tux tux May 30 '15 at 08:17

1 Answers1

0

You never assigned the entry1 variable from your GtkBuilder, so entry1 is still NULL at the time of the signal, and the program crashes. You did it for window; doing it for entry1 is similar.

andlabs
  • 11,290
  • 1
  • 31
  • 52