Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Sortable table using Gtk2 in UI application

by SyneRohit (Initiate)
on Sep 30, 2011 at 11:58 UTC ( #928753=perlquestion: print w/ replies, xml ) Need Help??
SyneRohit has asked for the wisdom of the Perl Monks concerning the following question:

Hello People, I need to display spreadsheet kind of data in Perl UI. The data should be sortable for some columns. I am using Perl Gtk2 for UI. I am reading the data from the file and I need to display it on UI. Please help me on this.

Comment on Sortable table using Gtk2 in UI application
Re: Sortable table using Gtk2 in UI application
by choroba (Abbot) on Sep 30, 2011 at 12:00 UTC
    What exactly is your problem? Just keep the data in memory and sort them and redisplay upon request.
Re: Sortable table using Gtk2 in UI application
by zentara (Archbishop) on Sep 30, 2011 at 15:17 UTC
    Choroba's advice is the easiest. But if you want an example of sorting the Treeview, here is one. Setting up Gtk2 is complicated.
    #! /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->new('toplevel'); $window->signal_connect('delete_event' => sub { Gtk2->main_quit; }); $window->set_border_width(5); $window->set_position('center_always'); #this vbox will geturn the bulk of the gui my $vbox = &ret_vbox(); #add and show the vbox $window->add($vbox); $window->show(); #our main event-loop Gtk2->main(); sub ret_vbox { my $vbox = Gtk2::VBox->new(FALSE,5); #create a scrolled window that will host the treeview my $sw = Gtk2::ScrolledWindow->new (undef, undef); $sw->set_shadow_type ('etched-out'); $sw->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->set_size_request (300, 300); #method of Gtk2::Container $sw->set_border_width(5); #this is one of the provided base Gtk2::TreeModel classes. my $tree_store = Gtk2::TreeStore->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->append(undef); $tree_store->set ($iter,0 => "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->append($iter); $tree_store->set ($iter_child,0 => "Child $_ of Parent $parent_nr"); } } #this will create a treeview, specify $tree_store as its model my $tree_view = Gtk2::TreeView->new($tree_store); #create a Gtk2::TreeViewColumn to add to $tree_view my $tree_column = Gtk2::TreeViewColumn->new(); $tree_column->set_title ("Click to sort"); #create a renderer that will be used to display info #in the model my $renderer = Gtk2::CellRendererText->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->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->add_attribute($renderer, text => 0); #add $tree_column to the treeview $tree_view->append_column ($tree_column); # make it searchable $tree_view->set_search_column(0); # Allow sorting on the column $tree_column->set_sort_column_id(0); # Allow drag and drop reordering of rows $tree_view->set_reorderable(TRUE); $sw->add($tree_view); $vbox->pack_start($sw,TRUE,TRUE,0); $vbox->show_all(); return $vbox; }

    I'm not really a human, but I play one on earth.
    Old Perl Programmer Haiku ................... flash japh
Re: Sortable table using Gtk2 in UI application
by zentara (Archbishop) on Sep 30, 2011 at 17:34 UTC
    Here is a simpler way to sort a list, using SimpleList. There are other ways too.
    #!/usr/bin/perl use warnings; use strict; use Gtk2 -init; use Gtk2::SimpleList; # click on the "int" column header to sort on it my $win = Gtk2::Window->new; $win->signal_connect (delete_event => sub { Gtk2->main_quit; }); my $vbox = Gtk2::VBox->new; $win->add ($vbox); my $slist = Gtk2::SimpleList->new ( 'Int' => 'int', 'Text' => 'text' ) +; @{$slist->{data}} = ( [11, 'text1'], [21, 'text2'], [3, 'text3'] ); $slist -> set_reorderable( 1 ); #my @columns = $slist->get_columns; #for (my $i = 0 ; $i < @columns ; $i++) { #$columns[$i]->set_sort_column_id ($i); #} # or $slist->get_column (0)->set_sort_column_id (0); #$slist->get_column (1)->set_sort_column_id (1); #in case you want to catch the sort occuring $slist->get_column(0)->signal_connect( clicked => sub { warn "clicked!\n" } ); $vbox->add ($slist); $win->show_all; Gtk2->main;

    I'm not really a human, but I play one on earth.
    Old Perl Programmer Haiku ................... flash japh
      Thank you. This solution worked for me.
        Your welcome. After posting that example code, I extended it to multicolumn sorting. You may find this example even better. It sorts on each column, by clicking that column header.
        #!/usr/bin/perl use warnings; use strict; use Gtk2 -init; use Gtk2::SimpleList; my $win = Gtk2::Window->new; $win->signal_connect (delete_event => sub { Gtk2->main_quit; }); my $vbox = Gtk2::VBox->new; $win->add ($vbox); my $slist = Gtk2::SimpleList->new ( 'Int' => 'int', 'Text' => 'text' ) +; @{$slist->{data}} = ( [11, 'text1'], [21, 'text2'], [3, 'text3'], [41, + '4text'] ); $slist -> set_reorderable( 1 ); #my @columns = $slist->get_columns; #for (my $i = 0 ; $i < @columns ; $i++) { #$columns[$i]->set_sort_column_id ($i); #} # or $slist->get_column (0)->set_sort_column_id (0); $slist->get_column (1)->set_sort_column_id (1); $slist->get_column(0)->signal_connect( clicked => sub { warn "clicked 0!\n" } ); $slist->get_column(1)->signal_connect( clicked => sub { warn "clicked 1!\n" } ); $vbox->add ($slist); my $button = Gtk2::Button->new('Show Data'); $vbox->pack_end( $button, 0, 0, 0 ); $button->signal_connect( clicked => sub { print @{$slist->{data}},"\n"; # demonstrates reordering foreach my $arr ( @{$slist->{data}} ){ print "$arr->[0]\t$arr->[1]\n" } print "\n\n"; } ); $win->show_all; Gtk2->main;

        I'm not really a human, but I play one on earth.
        Old Perl Programmer Haiku ................... flash japh

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://928753]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (10)
As of 2014-11-27 17:24 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My preferred Perl binaries come from:














    Results (186 votes), past polls