-1

我正在 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) 的行,以便在用户再次单击复选框时隐藏它们(停用)?

4

1 回答 1

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  ----------
于 2012-12-09T12:39:39.277 回答