سؤال

ولدي جتك :: TreeView مع جتك :: TreeModel وجتك :: TreeModelFilter. شجرة نموذج مثل هذا:

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

وأريد أن تصفية على محتوياتsearch_entry، ولكن أريد category1 للظهور إذا على التوالي في إطار ذلك لا تزال واضحة، وcategory2 تكون مخفية إذا كان هناك أية صفوف تحته تزال واضحة. فهمي جتك :: TreeModelFilter # set_visible_func هو أن تحصل على نموذج وايتر من "نموذج الطفل"، بحيث يمكنك التحقق ما إذا كان لعرض ايتر الطفل. يحصل على استدعاء هذه الدالة على كل ايتر في النموذج في كل مرة أدعو جتك :: TreeModelFilter # refilter.Therefore أنا أقول: إذا كان ايتر ما قدمتموه لي فقط على المستوى الأول، احصل على الطريق، خطوة واحدة إلى الأسفل، لتحويل نفس المسار على نموذج مرشح واستخدام ما إذا كان المسار الجديد موجود لرؤية الاختبار.

@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

ويسبب "هذا الخطأ: مستوى كومة عميق جدا" الإخراج لكل ايتر.

وهناك من العودية لانهائية يجري هنا، لكني لا أرى فيها هذا يحدث أو كيف يمكن تجنب ذلك. أنا متأكد من أني أفكر في هذا الطريق الخاطئ، ولكن لقد تم القرصنة على هذه بضعة أيام دون انفراجة.

هل كانت مفيدة؟

المحلول

و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 مع الإسناد
لا تنتمي إلى StackOverflow
scroll top