Re: How to remove selected items



Hey Robert:

Actually, the API for handling multiple selections is not particularly
vile.  But, finding all the bits and pieces in the docs can be
challenging sometimes.

I am going to assume that you have created your Gtk::ListStore and your
Gtk::TreeView and a Gtk::TreeSelection from that Gtk::TreeView and that
you have called Gtk::TreeSelection::set_mode to allow for selection of
multiple rows in your Gtk::TreeView.  Furthermore, I am going to assume
that you have connected a callback slot to
Gtk::TreeView::signal_keypress_event that will be called when your user
presses the delete key when your Gtk::TreeView has focus.  You may also
need to call Gtk::TreeView::add_events using GDK_KEY_RELEASE_MASK.

Furthermore, I am going to assume that you have written a function in
your class that will serve as the callback slot parameter for  
Gtk::TreeSelection::selected_foreach_iter.  In this callback slot you
should make a call to Gtk::TreeModel::erase using the iterator passed as
its formal parameter, and voila!  Your selected rows will be deleted. 
The relevant code would look something like this:

class MyClass : public Gtk::Window {
public:
    MyClass();
    ~MyClass();
private:
    Glib::RefPtr<Gtk::ListStore> refList;
    Gtk::TreeView* pTreeView;
    Glib::RefPtr<Gtk::TreeSelection> refSel;
    void foreach_selected(const Gtk::TreeModel::iterator& iter);
    bool on_keypressed(GdkEventKey* keyevent);
};

bool MyClass::on_keypressed(GdkEventKey* keyevent) {
    if(keyevent->type == GDK_KEY_RELEASE && keyevent->keyval ==
GDK_DELETE) {
       
refSel->selected_foreach_iter(sigc::mem_fun(*this,&MyClass::foreach_selected));
        return TRUE;
    }
    return FALSE;
}

void MyClass::foreach_selected(const Gtk::TreeModel::iterator& iter) {
    refList->erase(iter);
}


Disclaimer:

If you guys see any glaring (or even not so glaring) mistakes, I expect
you to point them out!  :)

Bob

Robert Pearce wrote:
> Guys,
>
> I have a question. Actually I have two, but I'll put the other in a 
> separate thread.
>
> In my application I have a TreeView showing a ListStore. The view is 
> configured with SELECT_MULTIPLE, because I want the user to be able to 
> select a number of rows and hit delete. When he does so, it should do 
> the obvious thing and remove all the selected rows.
>
> The question is, how do I do this? The API for handling multiple 
> selections looks truly vile! Actually, I've already kind-of had to do it 
> elsewhere, but in that case I was doing something it fitted rather 
> better. Trying to use the multiple selection API to remove selected 
> items from the list seems unnecessarily painful. And it seems like the 
> sort of thing lots of applications might do, so I wondered if anyone had 
> a nice neat trick for doing it in two lines :)
>
> Thanks,
>   




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