Ordenar

Los modelos de árbol estándar (TreeStore y ListStore) derivan de TreeSortable, por lo que ofrecen funciones de ordenación. Por ejemplo, llame a set_sort_column() para ordenar el modelo por la columna especificada. O, proporcione una función de retorno de llamada a set_sort_func() para implementar un algoritmo de ordenación más complejo.

TreeSortable Reference

10.5.1. Ordenación al pulsar en columnas

So that a user can click on a TreeView's column header to sort the TreeView's contents, call Gtk::TreeView::Column::set_sort_column(), supplying the model column on which model should be sorted when the header is clicked. For instance:

auto pColumn = treeview.get_column(0);
if(pColumn)
  pColumn->set_sort_column(m_columns.m_col_id);

10.5.2. Vistas ordenadas independientemente del mismo modelo

The TreeView already allows you to show the same TreeModel in two TreeView widgets. If you need one of these TreeViews to sort the model differently than the other then you should use a TreeModelSort instead of just, for instance, Gtk::TreeViewColumn::set_sort_column(). TreeModelSort is a model that contains another model, presenting a sorted version of that model. For instance, you might add a sorted version of a model to a TreeView like so:

auto sorted_model = Gtk::TreeModelSort::create(model);
sorted_model->set_sort_column(columns.m_col_name, Gtk::SORT_ASCENDING);
treeview.set_model(sorted_model);

Tenga en cuenta, sin embargo, que el «TreeView» le proporcionará iteradores al modelo ordenado. Debe convertirlos a iteradores del modelo hijo subyacente para llevar a cabo acciones en ese modelo. Por ejemplo:

void ExampleWindow::on_button_delete()
{
  auto refTreeSelection = m_treeview.get_selection();
  if(refTreeSelection)
  {
    auto sorted_iter = m_refTreeSelection->get_selected();
    if(sorted_iter)
    {
      auto iter = m_refModelSort->convert_iter_to_child_iter(sorted_iter);
      m_refModel->erase(iter);
    }
  }
}

TreeModelSort Reference