<?xml version="1.0" encoding="windows-1252"?>
<node id="928831" title="Re: Sortable table using Gtk2 in UI application" created="2011-09-30 11:17:00" updated="2011-09-30 11:17:00">
<type id="11">
note</type>
<author id="131741">
zentara</author>
<data>
<field name="doctext">
[Choroba]'s advice is the easiest. But if you want an example of sorting the Treeview, here is one. Setting up Gtk2 is complicated.
&lt;c&gt;
#! /usr/bin/perl
use warnings;
use strict;
use Gtk2 '-init';
use Glib qw/TRUE FALSE/;

#standard window creation, placement, and signal connecting
my $window = Gtk2::Window-&gt;new('toplevel');
$window-&gt;signal_connect('delete_event' =&gt; sub { Gtk2-&gt;main_quit; });
$window-&gt;set_border_width(5);
$window-&gt;set_position('center_always');

#this vbox will geturn the bulk of the gui
my $vbox = &amp;ret_vbox();

#add and show the vbox
$window-&gt;add($vbox);
$window-&gt;show();

#our main event-loop

Gtk2-&gt;main();


sub ret_vbox {

my $vbox = Gtk2::VBox-&gt;new(FALSE,5);

#create a scrolled window that will host the treeview
my $sw = Gtk2::ScrolledWindow-&gt;new (undef, undef);
$sw-&gt;set_shadow_type ('etched-out');
$sw-&gt;set_policy ('automatic', 'automatic');
#This is a method of the Gtk2::Widget class,it will force a minimum
#size on the widget. Handy to give intitial size to a
#Gtk2::ScrolledWindow class object
$sw-&gt;set_size_request (300, 300);
#method of Gtk2::Container
$sw-&gt;set_border_width(5);

#this is one of the provided base Gtk2::TreeModel classes.
my $tree_store = Gtk2::TreeStore-&gt;new(qw/Glib::String/);

#fill it with arbitry data
my @data = qw(q e r g x u b k p a v );
foreach (@data) {

my $parent_nr = $_;
#the iter is a pointer in the treestore. We
#use to add data.
my $iter = $tree_store-&gt;append(undef);
$tree_store-&gt;set ($iter,0 =&gt; "Parent $parent_nr");

foreach (1..3){
#here we append child iters to the parent iter
#and add data to those chils iters.
my $iter_child = $tree_store-&gt;append($iter);
$tree_store-&gt;set ($iter_child,0 =&gt; "Child $_ of Parent $parent_nr");
}
}

#this will create a treeview, specify $tree_store as its model
my $tree_view = Gtk2::TreeView-&gt;new($tree_store);


#create a Gtk2::TreeViewColumn to add to $tree_view
my $tree_column = Gtk2::TreeViewColumn-&gt;new();

$tree_column-&gt;set_title ("Click to sort");

#create a renderer that will be used to display info
#in the model
my $renderer = Gtk2::CellRendererText-&gt;new;
#add this renderer to $tree_column. This works like a Gtk2::Hbox
# so you can add more than one renderer to $tree_column
$tree_column-&gt;pack_start ($renderer, FALSE);

# set the cell "text" attribute to column 0
#- retrieve text from that column in treestore
# Thus, the "text" attribute's value will depend on the row's value
# of column 0 in the model($treestore),
# and this will be displayed by $renderer,
# which is a text renderer
$tree_column-&gt;add_attribute($renderer, text =&gt; 0);

#add $tree_column to the treeview
$tree_view-&gt;append_column ($tree_column);

# make it searchable
$tree_view-&gt;set_search_column(0);

# Allow sorting on the column
$tree_column-&gt;set_sort_column_id(0);

# Allow drag and drop reordering of rows
$tree_view-&gt;set_reorderable(TRUE);

$sw-&gt;add($tree_view);


$vbox-&gt;pack_start($sw,TRUE,TRUE,0);
$vbox-&gt;show_all();
return $vbox;
}


&lt;/c&gt;

&lt;!-- Node text goes above. Div tags should contain sig only --&gt;
&lt;div class="pmsig"&gt;&lt;div class="pmsig-131741"&gt;
&lt;hr /&gt;
I'm not really a human, but I play one on earth.&lt;br&gt;
[id://630805] ................... &lt;a href=http://zentara.net/japh.html&gt; flash japh &lt;/a&gt;

&lt;/div&gt;&lt;/div&gt;</field>
<field name="root_node">
928753</field>
<field name="parent_node">
928753</field>
</data>
</node>
