GTK2 Perl:从表中删除/隐藏一行(TreeViewColumn)

时间:2012-12-07 08:35:53

标签: perl user-interface treeview gtk2

我在gtk2 perl上编写GUI应用程序。当用户单击复选框时,有一个表格显示数据库中的行。当他再次点击它时,必须隐藏具有已定义列值的行。

我有这段代码:

# ListStore to stores model
my $list_store = Gtk2::ListStore->new(("Glib::String") x 3);

my $rows = &get_row($st); # arrayref on rows from a db

sub set_columns {
    my ($store, $rows) = @_;

    foreach my $row (@$rows) {
        my ($num, $name, $status) = @$row;
        $store->set($store->append,
            0 => $num,
            1 => $name,
            2 => $status,
        );
    }
}   # ----------  end of subroutine set_columns  ----------

...

sub show_columns {
    my ($names) = @_;                        # reference to @name_columns
    my $i = 0;                               # number of columns within ListStore
    foreach (@$names) {
        # TreeViewColumn is a column for TreeView
        my $col = Gtk2::TreeViewColumn->new;
        $col->set_title($_);
            $col->set_alignment(0.5);            # alignment of header
            $col->set_clickable(1);              # can click on header


        $tree_view->append_column($col);

        my $rend = Gtk2::CellRendererText->new;
        $col->pack_start($rend, TRUE);

        # Link column of TreeViewColumn's renderer to column of ListStore
        $col->add_attribute($rend, 'text', $i++);
    }
}

...

my @name_columns = qw(№ Name Status);

&set_columns($store, $rows);
&show_columns(\@name_columns);

我有一个复选框:

$ch->signal_connect(toggled => sub {
    my $self = shift;
    if ($self->get_active) {
        print "Yes\n";
                my $st = 'Good';       # status 
        &global_view($list_store, $st);  # function which call set and show_columns
    } else { 
        print "No\n";
                # TO-DO here!!!!!
    }
});

那么,如何从TreeViewColumn中隐藏已定义$ st(status)的行,以便在用户再次单击复选框时将其隐藏起来(停用)?

1 个答案:

答案 0 :(得分:0)

我已经解决了这个问题!我使用这段代码:

sub remove_item {
    my ($st)= @_;
    my $it = $list_store->get_iter_first;
    while ($it) {
        if ($list_store->iter_is_valid($it) && $list_store->get($it, 3) eq $st) {
            my $tmp = $it;  # temporary iter for row delete
            print "Good\n";
            $it = $list_store->iter_next($it);
            $list_store->remove($tmp);
        } else {
            print "Bad\n";          
            $it = $list_store->iter_next($it);      
        }
    }   

}   # ----------  end of subroutine remove_item  ----------