Re: [gtkmm] going crazy with the multipe selection with Gtk::TreeSelection



cedric wrote:

Hello!
yes, it s still me with another new problem :p with Gtkmm 2.2 (not 2.4 for the moment)

this time, i ve made a Gtk::ListStore with several rows.
i ve add a button witch can add a new row, and another witch can delete the highlighted (= selectionned row). it works, so i ve tried to enable multiple selection (this is ok) and then to enable multiple supression and this last thing is my problem.

i ve read the docs of gtkmm.org about it, and i ve made my callback and the multiple selection as explained, but i don t manage to compile my prog! in the examples i found

here is the doc explanations:

------------------------------------------------------------------------------ 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.
}
------------------------------------------------------------------------------

I imagine that the first instruction is supposed to connect each selected row to the &TheClass::selected_row_callback to "do a thing" with each row separately. but the callback has an iter parameter and THIS is my problem: i don t manage to put it in the first instruction...

here is a part of my source:

------------------------------------------------------------------------
//to enable multiple rows selection
    refTreeSelection = billArray.get_selection();
    refTreeSelection->set_mode(Gtk::SELECTION_MULTIPLE);

...


//called when we press the "delete_lines" button
void BillFormat::on_delline_press()
{    // ok for single selection
/*    refTreeSelection = billArray.get_selection();
    Gtk::TreeModel::iterator iter;
    iter = refTreeSelection->get_selected();
    row = *(m_refListModel->erase(iter));*/

    //for multiple selection
    refTreeSelection->selected_foreach
        (SigC::slot(*this, &BillFormat::selected_row_callback(iter) ) );
}


void BillFormat::selected_row_callback(Gtk::TreeModel::iterator iter)
{
//    Gtk::TreeModel::iterator iter;
// iter = refTreeSelection->get_selected(); Gtk::TreeModel::Row row = *iter;

    row = *(m_refListModel->erase(iter));
}
-------------------------------------------------------------------------------------

i ve tried many ways to pass the iter parameter without any success...

You don't need to. The parameter is provided by selected_foreach() when it calls your callback function, and it will be an iterator that points to the current selected row. This is how your code can work on each selected row in turn.

I'd suggest reviewing the sections of the gtkmm tutorial/book on signals and callbacks, as this kind of parameter passing in signals is very common, so if you don't understand it you will end up banging your head on things for a while yet.

So really your connection code needs to be

refTreeSelection->selected_foreach(SigC::slot(*this, &BillFormat::selected_row_callback));

And your callback:

void BillFormat::selected_row_callback(Gtk::TreeModel::iterator &iter) {
  Gtk::TreeModel::Row row = *iter;
  row = *(mrefListModel->erase(iter));
}

Which should work, although I'm sure there's a slightly more elegant way to delete all selected rows. I'll check in my own code that does this later, if I remember.




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