2015-06-15 83 views
1

我有一個簡單的GtkTreeViewGtkEntry用於過濾模型。 當我在輸入中輸入somehing時,software_list被語言過濾。GtkTreeView過濾和選擇

software_list = [("Firefox", 2002, "C++"), 
       ("Eclipse", 2004, "Java"), 
       ("Netbeans", 1996, "Java"), 
       ("Chrome", 2008, "C++"), 
       ("GCC", 1987, "C"), 
       ("Frostwire", 2004, "Java")] 

class TreeViewFilterWindow(Gtk.Window): 

    def __init__(self): 
     Gtk.Window.__init__(self) 
     self.curr_filter = '' 

     self.entry = Gtk.Entry() 
     self.entry.connect('changed', self.on_text_change) 

     self.software_liststore = Gtk.ListStore(str, int, str) 
     for software_ref in software_list: 
      self.software_liststore.append(list(software_ref)) 

     self.filter = self.software_liststore.filter_new() 
     self.filter.set_visible_func(self.filter_func) 

     self.treeview = Gtk.TreeView.new_with_model(self.filter) 
     for i, column_title in enumerate(["Software", "Release Year", "Programming Language"]): 
      renderer = Gtk.CellRendererText() 
      column = Gtk.TreeViewColumn(column_title, renderer, text=i) 
      self.treeview.append_column(column) 
     self.treeview.get_selection().connect('changed', self.on_row_select) 

     # packing into boxes, showing components, starting main loop goes here 

    def on_text_change(self, entry): 
     self.curr_filter = entry.get_text() 
     self.filter.refilter() 

    def filter_func(self, model, iter, data): 
     if self.curr_filter: 
      return re.search(re.escape(self.curr_filter), model[iter][2]) 
     else: 
      return True 

的問題是,當我從列表中選擇即「鉻」,然後鍵入「Java」的進入入口,然後,很明顯,「鉻」得到隱藏的,但選擇更改爲一些其他的,隨機行。我更喜歡TreeView取消選擇隱藏的元素,而不是改變選擇。我怎樣才能做到這一點?

+0

這是非常令人費解的代碼。你真的想在這裏完成什麼? 「那麼,顯然,」Chrome「隱藏起來」對我來說並不明顯。 – jcoppens

回答

0

這只是在Gtk2中按預期工作,但在Gtk3中,如果消失,您需要取消選擇該行。適當的代碼是

class TreeViewFilterWindow(Gtk.Window): 

    def __init__(...): 
     ... 
     self.selection = self.treeview.get_selection() 
     self.filter.connect('row-deleted', self.on_row_deleted) 

    def on_row_deleted(self, model, path): 
     if self.selection.path_is_selected(path): 
      GObject.idle_add(self.selection.unselect_path, path) 

我發現,直接調用self.selection.unselect_path(path)似乎沒有出於某種原因,但與idle_add推遲它整理出來。