Re: [gtkmm] TreeView/TreeModel navigation



On Friday 29 November 2002 4:17 am, 3rdShift wrote:
> Hi,
>
> I am re-reading TreeView/TreeModel reference, docmentation and demo
> examples again and again and can't seem to find a way to navigate
> through ListStore. With CList I used to get a row number when user
> selects the row.
>
> 1) How do I get the selected row number?
>
> In the selection callback I can get to the row, but how far is
> that row from the begin()?
>
> Glib::RefPtr<Gtk::TreeSelection> m_tree_sel_ref;
> Glib::RefPtr<Gtk::ListStore> m_list_store_ref;
>
> Gtk::TreeModel::iterator iter = m_tree_sel_ref->get_selected ();
> Gtk::TreeModel::Row row = *iter;
>
>
> I tried to subtract iterators just like you can do in STL, but
> all I get is 0!
>
> int row_num = iter - m_list_store_ref->children ().begin ()
>
> 2) How can I select the previous row?
>
> Gtk::TreeIter is a one-directional forward iterator.
> Any hints?
>
> 3) How can I know if the TreeIter points to the last row
> in ListStore?
>
> I have an iterator to the currently selected row. I cannot say
>
> if (iter + 1 != m_list_store_ref->children ().begin ()) {
>     m_tree_sel_ref->select (++iter);
> }
>
> because TreeIter does not support operator+(int).
> However, if I increment the iterator and hit the end of the list,
> I get gtk-CRITICAL assertion:
>
> TreeView-Test:27034): Gtk-CRITICAL **: file gtkliststore.c: line 518
> (gtk_list_store_get_path): assertion `iter->stamp == GTK_LIST_STORE
> (tree_model)->stamp' failed

I have had very similar problems.  To find the row immediately before the 
selected row I had to have two Gtk::TreeModel::Children::iterator objects, 
one less advanced than the other, and then iterate through the list store 
from the beginning until the more advanced one equalled 
Gtk::TreeView::get_selected()->selection().  I imagine there must be a better 
way of doing it (it is is quite inefficient with a large list), but I 
couldn't work it out from the documentation (such as it is).  It is 
surprising that a Gtk::ListStore object doesn't have bidirectional interators 
(although I can see that may be a problem with a Gtk::TreeStore object).

Finding out if the iterator returned by 
Gtk::TreeView::get_selected()->selection() points to the last row seemed 
rather easier.  Copy it to another Gtk::TreeModel::iterator (or to a 
Gtk::TreeModel::Children::iterator), increment the copy, and see if it equals 
Gtk::ListStore::children()->end().  It worked for me, but it wouldn't 
surprise me if there is a better way.  (I do not understand the error you 
report: that doesn't seem right, so there may be some other problem with your 
code).

Chris.




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