簡體   English   中英

堆棧級別太深錯誤Ruby-Gnome2

[英]Stack level too deep error Ruby-Gnome2

我有一個Gtk :: TreeView和一個Gtk :: TreeModel和一個Gtk :: TreeModelFilter。 樹模型是這樣的:

category1
  --> actual row of data
category2
  --> actual row of data

我想對@search_entry的內容進行過濾,但是我希望如果在它下面的行仍然可見的情況下顯示category1,並且在它下面沒有行的情況下將category2隱藏。 我對Gtk :: TreeModelFilter#set_visible_func的理解是,您可以從“子模型”中獲取模型和迭代器,以便可以檢查是否顯示子迭代器。 每次我調用Gtk :: TreeModelFilter#refilter時,都會在模型中的每個迭代上調用此函數。因此,我的意思是:如果您剛剛給我的迭代位於第一層,請獲取路徑,下移路徑,轉換為過濾器模型上的相同路徑,並使用是否存在新路徑來測試可見性。

@store = Gtk::TreeStore.new(Gdk::Pixbuf, String, String, Menagerie::Program, TrueClass)
@tree_filter = Gtk::TreeModelFilter.new(@store)
@treeview.model = @tree_filter

# @first_time gets set to false after the model is loaded the first time
@first_time = true
@tree_filter.set_visible_func do |model, iter|
  has_visible_children = true
  begin
    iter_path = iter.path
    if iter_path.depth == 1 && @first_time != true
      iter_path.down!
      has_visible_children = @tree_filter.convert_child_path_to_path(iter_path) ? true : false
    end
  rescue => e
    puts "THIS ERROR: " + e.message
  end
  unless @search_entry.text == ""
    if [1,2].collect {|i| iter[i] =~ /#{@search_entry.text}/i }.any?
      true
    elsif iter[4] == true and has_visible_children
      true
    else
      false
    end
  else
    true
  end
end

has_visible_children = @tree_filter.convert_child_path_to_path(iter_path) ? true : false

導致每個迭代的“此錯誤:堆棧級別太深”輸出。

這里有無限遞歸,但是我不知道它在哪里發生或如何避免。 我敢肯定我在想這是錯誤的方法,但是我在這方面做了大量的工作卻沒有取得突破。

我對Ruby一無所知,但是這個錯誤顯然指出了太多的遞歸迭代。 需要將每次調用的上下文存儲在堆棧中,從而導致-歡呼-

堆棧溢出

:-)添加一個變量來跟蹤您的迭代級別並打印出錯誤。 您的數據或遞歸邏輯有問題,或兩者都有。

refilter在每個節點上調用該塊。 不過,返回值並未保存在節點中,因此,無論如何執行,如果必須向下看樹,都將重復計算。

# Simplified version - returns true if search_text found in iter or any of its
# first-level children.
# Let's assume you add a method to GTK::TreeIter:
#    def has_text? search_text
#      self[1] =~ /#{search_text}/i or self[2] =~ /#{search_text}/i
#    end
@tree_filter.set_visible_func do |model, iter|
  next true if @search_entry.text.empty?   # No filtering if no search text
  next true if iter.path.depth == 0        # Always show root node
  next true if iter.has_text? @search_entry.text

  if child_iter = iter.first_child # Then we have children to check
    has_visible_children = false
    loop do 
      has_visible_children ||= child_iter.has_text? @search_entry.text
      break unless child_iter.next! # returns false if no more children
    end
    next has_visible_children
  end

  next false # Not root, doesn't contain search_text, has no children
end 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM