Re: how to remove selected rowS from a ListStore?



On 10/23/07, Sergio Perticone <g4ll0ws gmail com> wrote:
> On Tue, 2007-10-23 at 19:15 +0800, Liangxu Wang wrote:
> > Use iconview object, delete the selected items, the code:
> > ------------
> > //if use the treeview, replace the get_selected_items() with
> > get_selection().
> >       std::list<Gtk::TreeModel::Path> selected = get_selected_items();
> > -- snip --
>
> Thanks, my list now works, but I'm blocked.. I don't know the next step.
> My newer code is:
>
> typedef std::list<Gtk::TreePath> ListPath;
>
> void TreeView::on_remove()
> {
>     Glib::RefPtr<Gtk::TreeModel> model = get_model();
>     ListPath list = get_selection()->get_selected_rows(model);
>
>     // and now?
> }
>
> Gtkmm Reference about get_selected_rows() says:
> ----
> if you are planning on modifying the model after calling this function,
> you may want to convert the returned list into a list of
> GtkTreeRowReferences.
> ----
>
> Well... How I should convert my list?
>
> Thanks,

Basically, a TreeRowReference stays valid even if the model changes,
whereas iterators are invalidated anytime the model is changed, so
when you erase the first iterator, all of your other iterators will
become invalid and you won't be able to delete them all.
Here's a code sample from one of my projects:

            Glib::RefPtr<Gtk::TreeSelection> sel = get_selection();
            std::list<Gtk::TreeModel::Path> paths = sel->get_selected_rows();

            // convert all of the paths to RowReferences
            std::list<Gtk::TreeModel::RowReference> rows;
            for (std::list<Gtk::TreeModel::Path>::iterator pathiter =
paths.begin();
                    pathiter != paths.end(); pathiter++)
            {

rows.push_back(Gtk::TreeModel::RowReference(get_model(), *pathiter));
            }

            // remove the rows from the treemodel
            for (std::list<Gtk::TreeModel::RowReference>::iterator i =
rows.begin();
                    i != rows.end(); i++)
            {
                Gtk::TreeModel::iterator treeiter =
m_model->get_iter(i->get_path());
                m_model->erase(treeiter);
            }

-- 
jonner


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