Re: a TreeModel that's sortable and filterable?



Jamf wrote:
Armin Burgmeier escribió:
TreeModelFilter and TreeModelSort have child models. So, if you have
some TreeModel A, and you want it sorted, then create a TreeModelSort
with A as child. If you want it both sorted and filtered, then create a
TreeModelFilter with a TreeModelSort as child (or vice versa).

Note that Gtk::ListStore and Gtk::TreeStore already support sorting
(since they implement Gtk::TreeSortable), so you don't even need
Gtk::TreeModelSort for them (unless you want to do things like showing a
sorted and an unsorted model in different views).
I have similar situation in my project.
If I put ListStore as the model in the treeview, then the sorting properties work correctly, but when a TreeModelFilter (with the liststore as child) is used as the model in the treeview, the ListStore's sorting properties don't work and I get: "Gtk-CRITICAL **: gtk_tree_sortable_get_sort_column_id: assertion `GTK_IS_TREE_SORTABLE (sortable)' failed Gtk-CRITICAL **: gtk_tree_sortable_has_default_sort_func: assertion `GTK_IS_TREE_SORTABLE (sortable)' failed" Then the only way to get sorting properties is creating a new TreeModelSort with the TreeModelFilter as child.


That's really strange. Just out of curiosity, I modified the ListStore example found here <http://www.gtkmm.org/docs/gtkmm-2.4/examples/book/treeview/list/> with the attached patch to sort its rows and use a TreeModelFilter with the ListStore as its child to exclude the row with id 4 and it works fine. Would you be able to do something similar?

--
José Alburquerque
jaalburquerque cox net

Index: examplewindow.cc
===================================================================
--- examplewindow.cc	(revision 998)
+++ examplewindow.cc	(working copy)
@@ -45,8 +45,18 @@
 
   //Create the Tree model:
   m_refTreeModel = Gtk::ListStore::create(m_Columns);
-  m_TreeView.set_model(m_refTreeModel);
 
+  //Set the sort column of the Tree model:
+  m_refTreeModel->set_sort_column(0, Gtk::SORT_ASCENDING);
+
+  //Create a TreeModelFilter from the ListStore and set its visible function:
+  m_refTreeModelFilter = Gtk::TreeModelFilter::create(m_refTreeModel);
+  m_refTreeModelFilter->set_visible_func( sigc::mem_fun(*this,
+             &ExampleWindow::on_is_visible) );
+
+  //Set the TreeView's model to the TreeModelFilter:
+  m_TreeView.set_model(m_refTreeModelFilter);
+
   //Fill the TreeView's model
   Gtk::TreeModel::Row row = *(m_refTreeModel->append());
   row[m_Columns.m_col_id] = 1;
@@ -55,17 +65,23 @@
   row[m_Columns.m_col_percentage] = 15;
 
   row = *(m_refTreeModel->append());
+  row[m_Columns.m_col_id] = 3;
+  row[m_Columns.m_col_name] = "Rob McRoberts";
+  row[m_Columns.m_col_number] = 30;
+  row[m_Columns.m_col_percentage] = 70;
+
+  row = *(m_refTreeModel->append());
+  row[m_Columns.m_col_id] = 4;
+  row[m_Columns.m_col_name] = "Zane Smith";
+  row[m_Columns.m_col_number] = 50;
+  row[m_Columns.m_col_percentage] = 30;
+
+  row = *(m_refTreeModel->append());
   row[m_Columns.m_col_id] = 2;
   row[m_Columns.m_col_name] = "Joey Jojo";
   row[m_Columns.m_col_number] = 20;
   row[m_Columns.m_col_percentage] = 40;
 
-  row = *(m_refTreeModel->append());
-  row[m_Columns.m_col_id] = 3;
-  row[m_Columns.m_col_name] = "Rob McRoberts";
-  row[m_Columns.m_col_number] = 30;
-  row[m_Columns.m_col_percentage] = 70;
-
   //Add the TreeView's view columns:
   //This number will be shown with the default numeric formatting.
   m_TreeView.append_column("ID", m_Columns.m_col_id);
@@ -109,3 +125,8 @@
   hide();
 }
 
+bool ExampleWindow::on_is_visible(const Gtk::TreeIter& iter)
+{
+  int id = (*iter)[m_Columns.m_col_id];
+  return ( (id != 4) ? true : false);
+}
Index: examplewindow.h
===================================================================
--- examplewindow.h	(revision 998)
+++ examplewindow.h	(working copy)
@@ -30,6 +30,7 @@
 protected:
   //Signal handlers:
   virtual void on_button_quit();
+  virtual bool on_is_visible(const Gtk::TreeIter& iter);
 
   //Tree model columns:
   class ModelColumns : public Gtk::TreeModel::ColumnRecord
@@ -53,6 +54,7 @@
   Gtk::ScrolledWindow m_ScrolledWindow;
   Gtk::TreeView m_TreeView;
   Glib::RefPtr<Gtk::ListStore> m_refTreeModel;
+  Glib::RefPtr<Gtk::TreeModelFilter> m_refTreeModelFilter;
 
   Gtk::HButtonBox m_ButtonBox;
   Gtk::Button m_Button_Quit;


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]