GTK - How to update gtktextview/gtkentry? #C

437 Views Asked by At

i am creating a basic GROUND CONTROL STATION for a CubeSat, dividing it in two parts: COMMANDS (with no problems) and TELEMETRY. I am using C code and GTK; in the telemetry windowt I need to show some info everytime a telemetry packet is received so I've tried to use a gtkview/gtkentry for each info, but I don't know, how to update the message shown in them.

In particular, an example is:

//View PACKET NUMBER    
 view = gtk_text_view_new();
 frame = gtk_frame_new("Packet number");    
 gtk_container_add(GTK_CONTAINER(frame), view);
 gtk_text_view_set_editable(GTK_TEXT_VIEW(view),FALSE);
 gtk_table_attach(GTK_TABLE(table2),frame,0,1,0,1,GTK_FILL,GTK_FILL,5,5);
 buff = gtk_text_view_get_buffer(GTK_TEXT_VIEW(view));
 gtk_text_buffer_get_iter_at_offset(buff, &iter, 0);
 gtk_text_buffer_insert(buff, &iter,"waiting", -1);

so, first there is the text "WAITING", then when a packet is received I want to update that text: how can I do this?

I've tried repeating this code but changing "waiting" with a variable referring to "packet number", but I obtain core dump

I've also tried with this code, but I have always same problem.

viewprova = gtk_entry_new();
frameprova = gtk_frame_new("Packet number");
gtk_container_add(GTK_CONTAINER(frameprova), viewprova);
gtk_entry_set_editable(GTK_ENTRY(viewprova),FALSE);
gtk_table_attach(GTK_TABLE(table2),frameprova,0,1,0,1,GTK_FILL,GTK_FILL,5,5);
gtk_entry_set_text(GTK_ENTRY(viewprova),"waiting");



frameprova = gtk_frame_new("Packet number");    
viewprova = gtk_label_new ("waiting");
gtk_container_add(GTK_CONTAINER(frameprova), viewprova);
gtk_table_attach(GTK_TABLE(table2),frameprova,0,1,0,1,GTK_FILL,GTK_FILL,5,5);   

Thanks for the help!

2

There are 2 best solutions below

1
On

Ok, so if I understood you correctly, you know how to setup your text displaying widgets, but not, how to fill them with new contents, correct? Look at your code, and at what you're doing. First, you're creating a text widget. Then you fill it with initial text. This second part is the one you repeat:

In case of GtkEntry, gtk_entry_set_text(GTK_ENTRY(viewprova), "My new text");

In case of GtkTextView (actually you're using the underlying TextBuffer), gtk_text_buffer_set_text(buff, "My new text", -1);

1
On

A function such as gtk_label_new() that accepts a C string can't take an integer instead, C functions are not polymorphic. You need to build a string representation and pass that, for instance using snprintf() to format the number into a string buffer.