GtkTreeView Constructor

GtkTreeView ([GtkTreeModel model]);

Creates a new GtkTreeView, optionally with a data store. If you don't set a it on construction, you can use set_model() after.

Example 139. GtkTreeview with sortable columns and some data

//the model
$store = new GtkListStore(Gobject::TYPE_BOOLEAN, Gobject::TYPE_STRING, Gobject::TYPE_LONG);

//add some data to the data store
$store->append(array(true , 'PHP'    , 1994));
$store->append(array(true , 'C'      , 1970));
$store->append(array(true , 'C++'    , 1983));
$store->append(array(false, 'Ruby'   , 1995));
$store->append(array(false, 'Python' , 1990));
$store->append(array(true , 'Java'   , 1994));
$store->append(array(false, 'Fortran', 1950));
$store->append(array(false, 'List'   , 1958));
$store->append(array(false, 'Haskell', 1987));
$store->append(array(false, 'Eiffel' , 1985));

//sort by year by default
$store->set_sort_column_id(2, Gtk::SORT_ASCENDING);

//We want to display our data in a GtkTreeView
$treeview = new GtkTreeView($store);

//the text renderer is used to display text
$cell_renderer = new GtkCellRendererText();

//Create the first column, make it resizable and sortable
$colLanguage = new GtkTreeViewColumn('Language', $cell_renderer, 'text', 1);
//make the column resizable in width
//make it sortable and let it sort after model column 1
//add the column to the view

//second column, also sortable
$colYear = new GtkTreeViewColumn('Year', $cell_renderer, 'text', 2);

//we want to display a boolean value, so we can use a check box for display
$bool_cell_renderer = new GtkCellRendererToggle();
$colUsed = new GtkTreeViewColumn('Used', $bool_cell_renderer, 'active', 0);

//A window where we can put our tree view
$wnd = new GtkWindow();
$wnd->set_title('Programming languages');
$wnd->connect_simple('destroy', array('gtk', 'main_quit'));

//to make the view scrollable, we need a scrolled window
$scrwnd = new GtkScrolledWindow();
$scrwnd->set_policy(Gtk::POLICY_AUTOMATIC, Gtk::POLICY_AUTOMATIC);

$wnd->set_default_size(250, 200);