Create  Edit  FrontPage  Index  Search  Changes  History  RSS  Login

tut-treeview-renderer-types

Gtk::CellRendererText and Integer, TrueClass 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 Gtk::TreeView#add_attributes 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 add a text cell renderer's "text" property to a model column, the model column does not need to be a String, it can also be one of several other classes, e.g. TrueClass, Integer, FixNum, or Float. The text cell renderer will automatically display the values of these types correctly in the tree view. For example:

list = Gtk::ListStore.new(String, TrueClass, Integer, FixNum, Float)
treeview = Gtk::TreeView.new(list)
# ...
renderer = Gtk::CellRendererText.new
col = Gtk::TreeViewColumn.new("Age", renderer, :text => 2)  # Note 3rd column in model is Integer, however,
                                                            # in view (renderer.text) is of type String
# ...
treeview.append_column(col)

Even though the "text" property would require a string value, we can use a model column of any of the boolean, integer, fix-num, or float types when setting attributes. Any of these types will then automatically be converted into a string before the cell renderer property is set.

If you are using a Float class, 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.

Last modified:2012/08/14 07:41:29
Keyword(s):
References:[tut-treeview]