【问题标题】:TreeModelFilter in GTK/Perl - Question about set_visible_funcGTK/Perl 中的 TreeModelFilter - 关于 set_visible_func 的问题
【发布时间】:2019-02-06 21:13:05
【问题描述】:

我正在尝试使用 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 ($unfiltered_store) 的第14 列,如果它是某个值,那么它会过滤到$filtered_store。

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

【问题讨论】:

  • 您需要传递对get_end_products 子例程的引用。所以试试$filtered_store->set_visible_func(\&get_end_products, $unfiltered_store)
  • 你的意思是这样? $filtered_store->set_visible_func(\&get_end_products, $unfiltered_store);
  • 仅供参考,即使在通过引用传递之前,“get_end_products”函数也会被调用。我的问题与商店有关——如何在“get_end_products”功能中访问商店?两个 dumper 语句什么都不打印,但是在 get_end_products 函数中打印警告语句(如“Hello World”)确实有效。
  • 嘿!我刚刚添加了参考并且......它有效!程序不会崩溃。真是太感谢你了。
  • 很高兴听到!请注意,变量$a$b 通常是sort 函数使用的特殊变量。所以不推荐使用那些变量名。

标签: perl treeview gtk gtk2


【解决方案1】:

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

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

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

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

关于您在 cmets 中的其他问题: 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) });

【讨论】:

  • 你太棒了。非常感谢。
  • 如果我使用匿名子,我如何访问迭代?我是否只是将它作为第二个参数传递给匿名子等?
  • 或者只是循环遍历模型等?
  • 是的,您可以尝试像这样 sub { get_end_products( $_[1], $unfiltered_store) } 传递它。现在第一个参数将是迭代,因为@_ 应该等于($model, $iter, $user_data)
  • 我无法检查模型迭代的值。执行 $val = $model->get_value($iter,$data) 将返回“无法定位对象方法 'get_value'..”错误。在返回真/假之前,我需要查看列的值。我做错了什么?
猜你喜欢
  • 1970-01-01
  • 2011-05-30
  • 2011-05-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多