0

I want to get the number of rows in a Treeview , after Filter has been applied . I tried using len(Gtk.ListStore) and Gtk.Liststore.iter_n_children() after filtering the rows but both of these functions returned Total number of rows that were there without filtering .

Is there any way I can get number of rows returned after filtering ?

EDIT : A snippet of my program as requested by Rawing :

    self.listfortreeview = Gtk.ListStore(str, str, str, str, str, str, str, str, str, str, str)
    for item in win.result:
        self.listfortreeview.append(list(item))

    self.current_ps_filter = None

    self.ps_filter = self.listfortreeview.filter_new()
    self.ps_filter.set_visible_func(self.ps_filter_func)

    self.ps_filter_sorted = Gtk.TreeModelSort(model=self.ps_filter)

    self.votertreeview = Gtk.TreeView.new_with_model(self.ps_filter_sorted)
    for i, coltitle in enumerate(["ID", "WARD", "PS NO", "SR NO", "NAME", "RELATION'S NAME", "SEX", "AGE", "ADDRESS",
                                  "PSTATION", "IDCARD NO"]):
        rendered = Gtk.CellRendererText(foreground = "blue")
        column = Gtk.TreeViewColumn(coltitle, rendered, text=i)
        column.set_sort_column_id(i)
        self.votertreeview.append_column(column)

    def filter_btn_clicked(self, button):
        self.get_typed_filter = self.filter_text.get_text().strip()
        if self.get_typed_filter == "":
            self.current_ps_filter = "None"
        else:
            self.current_ps_filter = self.get_typed_filter
        self.ps_filter.refilter()
        self.text_Filtered_records = "\tFiltered Records : " + str(self.listfortreeview.iter_n_children())
        self.label_Filtered_records.set_text(self.text_Filtered_records)
        self.label_Filtered_records.show()

    def ps_filter_func(self, model, iter, data):

        if self.current_ps_filter is None or self.current_ps_filter == "None":
            return True
        elif self.combo_text == "PS No.":
            return model[iter][2] == self.current_ps_filter

Here - win.result is a list of tuples

self.filter_text is an Entry in which user enters a string by which filter applies on 3rd column

self.combo_text is a Combo box by which user selects a column to perfrom on(for simplicity consider filter will be applied on 3rd column always)

Pawan Sharma
  • 1,842
  • 1
  • 14
  • 18
  • I'm not sure what this "Filter" is you're talking about. Could you post a short piece of code that creates a Treeview and applies a Filter to it? (See [mcve]) – Aran-Fey Jul 15 '17 at 15:12

1 Answers1

3

You're calling the function on the wrong object. Also, the builtin len function can be used instead of iter_n_children.

Instead of calling self.listfortreeview.iter_n_children() you simply have to call len(self.ps_filter). listfortreeview is the unfiltered ListStore object. Of course you'll always get the total number of elements from that.

Aran-Fey
  • 39,665
  • 11
  • 104
  • 149
  • Thanks a lot , It worked ! I tried using len and iter_n_child both , but as you mentioned , I was using them on wrong object that's why I was getting wrong number of rows . – Pawan Sharma Jul 15 '17 at 16:57