To find out what rows the user has selected, get the Gtk::TreeView::Selection object from the TreeView, like so:
Glib::RefPtr<Gtk::TreeSelection> refTreeSelection = m_TreeView.get_selection();
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);
For single-selection, you can just call get_selected(), like so:
TreeModel::iterator iter = refTreeSelection->get_selected(); if(iter) //If anything is selected { TreeModel::Row row = *iter; //Do something with the row. }
For multiple-selection, you need to define a callback, and give it to selected_foreach(), like so:
refTreeSelection->selected_foreach( SigC::Slot(*this, &TheClass::selected_row_callback) ); ... void TheClass::selected_row_callback(const Gtk::TreeModel::iterator& iter) { TreeModel::Row row = *iter; //Do something with the row. }
To respond to the user clicking on a row or range of rows, connect to the signal like so:
refTreeSelection->signal_changed().connect( SigC::slot(*this, &Example_StockBrowser::on_selection_changed) );
To change the selection, specify a Gtk::TreeModel::iterator or Gtk::TreeModel::Row, like so:
Gtk::TreeModel::Row row = m_refModel->children()[5]; //The fifth row. if(row) refTreeSelection->select(row);or
Gtk::TreeModel::iterator iter = m_refModel->children().begin() if(iter) refTreeSelection->select(iter);