GTK/Perl 中的 TreeModelFilter - 关于 set_visible_func 的问题

TreeModelFilter in GTK/Perl - Question about set_visible_func

我正在尝试使用 GTK2::TreeModelFilter 过滤列表存储。我似乎无法在网上找到使用 perl 的示例,并且出现语法错误。有人可以用下面的语法帮助我吗? $unfiltered_store 是一个列表存储。

$filtered_store = Gtk2::TreeModeFilter->new($unfiltered_store);
$filtered_store->set_visible_func(get_end_products, $unfiltered_store);
$combobox = Gtk2::ComboBoxEntry->new($filtered_store,1);

然后在下面某处:

 sub get_end_products {
      my ($a, $b) = @_;

      warn(Dumper($a));
      warn(Dumper($b));
      return true;     # Return all rows for now
 }

最终我想做的是查看listore的第14列($unfiltered_store),如果它是某个值,则将其过滤到$filtered_store.

有人可以帮我解决这个问题的语法吗?我检查了很多站点,但它们使用其他语言并使用不同的语法(例如 'new_filter' —— Perl GTK 不存在)。 这是我需要修复的最优雅的解决方案,我更愿意学习如何使用它,而不是使用蛮力方法来提取和保存过滤后的数据。

过滤存储的set_visible_func方法应该得到一个子引用作为第一个参数,但是你在这里没有传递子引用:

$filtered_store->set_visible_func(get_end_products, $unfiltered_store);

这将改为 调用 子例程 get_end_products,然后传递其 return 值(这不是子引用)。要修复它,请在子名称前添加引用运算符 \&

$filtered_store->set_visible_func(\&get_end_products, $unfiltered_store);

关于评论中的其他问题: According to the documentation 用户数据参数作为 第三个 参数传递给 get_end_products,因此您应该这样定义它:

sub get_end_products {
      my ($model, $iter, $user_data) = @_;
      # Do something with $user_data
      return TRUE;
 }

如果由于某种原因 $unfiltered_store 没有传递给 get_end_products,您可以尝试使用匿名 sub 传递它,如下所示:

$filtered_store->set_visible_func(
    sub { get_end_products( $unfiltered_store) });