La selección

Para descubrir qué filas ha seleccionado el usuario, obtenga el objeto Gtk::TreeView::Selection del TreeView, así:

auto refTreeSelection = m_TreeView.get_selection();

10.4.1. Selección única o múltiple

By default, only single rows can be selected, but you can allow multiple selection by setting the mode, like so:

refTreeSelection->set_mode(Gtk::SELECTION_MULTIPLE);

10.4.2. Las filas seleccionadas

Para la selección simple, puede simplemente llamar get_selected(), así:

auto iter = refTreeSelection->get_selected();
if(iter) //If anything is selected
{
  auto row = *iter;
  //Do something with the row.
}

For multiple-selection, you need to call get_selected_rows() or define a callback, and give it to selected_foreach(), selected_foreach_path(), or selected_foreach_iter(), like so:

refTreeSelection->selected_foreach_iter(
    sigc::mem_fun(*this, &TheClass::selected_row_callback) );

void TheClass::selected_row_callback(
    const Gtk::TreeModel::const_iterator& iter)
{
  auto row = *iter;
  //Do something with the row.
}

10.4.3. La señal «changed»

Para responder a la pulsación del usuario en una fila o un rango de filas, conéctese a la señal así:

refTreeSelection->signal_changed().connect(
    sigc::mem_fun(*this, &Example_IconTheme::on_selection_changed)
);

10.4.4. Evitar la selección de la fila

Tal vez, el usuario no deba ser capaz de seleccionar todos los elementos en su lista o árbol. Por ejemplo, en gtk-demo, puede seleccionar una demostración para ver su código fuente, pero no tiene ningún sentido seleccionar una categoría de demostraciones.

Para controlar qué filas pueden seleccionarse, use el método set_select_function(), proporcionándole un retorno de llamada sigc::slot. Por ejemplo:

m_refTreeSelection->set_select_function( sigc::mem_fun(*this,
    &DemoWindow::select_function) );

Y luego

bool DemoWindow::select_function(
    const Glib::RefPtr<Gtk::TreeModel>& model,
    const Gtk::TreeModel::Path& path, bool)
{
  const auto iter = model->get_iter(path);
  return iter->children().empty(); // only allow leaf nodes to be selected
}

10.4.5. Cambiar la selección

Para cambiar la selección, especifique un Gtk::TreeModel::iterator o un Gtk::TreeModel::Row, así:

auto row = m_refModel->children()[5]; //The sixth row.
if(row)
  refTreeSelection->select(row.get_iter());

o

auto iter = m_refModel->children().begin()
if(iter)
  refTreeSelection->select(iter);