5.4. GTree.cell_renderer_text and Integer, Boolean and Float Types

It has been said before that, when using attributes to connect data from the model to a cell renderer property, the data in the model column specified in GTree.view_column#add_attribute (gtk_tree_view_column_add_attribute) must always be of the same type as the data type that the property requires.

This is usually true, but there is an exception: if you use GTree.view_column#add_attribute (gtk_tree_view_column_add_attribute) to connect a text cell renderer's "text" property to a model column, the model column does not need to be of string, it can also be one of most other fundamental Gobject.Data types, e.g. boolean, int, uint, long, ulong, int64, uint64, float, or double. The text cell renderer will automatically display the values of these types correctly in the tree view. For example:


(* file: born.ml *)

open Gobject.Data

let cols = new GTree.column_list
let col_name = cols#add string
let col_born = cols#add uint

let create_model data =
  let store = GTree.tree_store cols in
  ...

let create_view ~packing () =
  ...
  let cell = GTree.cell_renderer_text [] in
  let col = GTree.view_column ~title:"Born"
      ~renderer:(cell, ["text", col_born]) () in
  ...

Even though the "text" property would require a model column of a string value, we use a model column of an integer type when setting attributes. The integer will then automatically be converted into a string before the cell renderer property is set.

If you are using a floating point type, ie. float or double, there is no way to tell the text cell renderer how many digits after the floating point (or comma) should be rendered. If you only want a certain amount of digits after the point/comma, you will need to use a cell data function.