0

how can we add a number of type gint to a TextBuffer in gtk+3? gtk_text_buffer_set_text has argument of type gchar but I want to set integer of type gint

user3318922
  • 51
  • 1
  • 6

2 Answers2

1

When doing I/O in C, you generally use strings. Since this is a form of I/O, it's to be expected that you need to format the number into a string first.

This is also nice since formatting a number into a string can be done many ways (different bases, number of digits, padding, and so on) so keeping this on the application side means the GTK+ widget doesn't have to know all that stuff.

The glib string utility functions API has a bunch of functions for dealing with strings. The most relevant here is probably g_snprintf():

void number_to_buffer(GtkTextBuffer *textbuf, int number)
{
  char buf[32];

  const gint len = g_snprintf(buf, sizeof buf, "%d", number);
  gtk_text_buffer_set_text(textbuf, buf, len);
}
unwind
  • 391,730
  • 64
  • 469
  • 606
0

You cannot show an integer value directly. You must first format the integer value in a character buffer and set this as the text

GtkTextBuffer *textbuf;
char cbuf[15];
int n, v;
v = 738;
n = sprintf(cbuf, "%d", v);
gtk_text_buffer_set_text(textbuf, cbuf, n);
Olaf Dietsche
  • 72,253
  • 8
  • 102
  • 198