Re: TreeModel selected row number

On Tue, 2005-03-22 at 10:07 -0700, John Taber wrote:
> On Tuesday 22 March 2005 09:35, Murray Cumming wrote:
> > > It is actually very useful if the tree is being used as any sort of list
> > > box.
> >
> > I don't understand. You have a list of colors, for example. You can find
> > out what color has been selected. Why do you need to know what
> > alphabetical sort position the color had in a list?
> >
> simply for flexibility and to avoid string comparisons 
> int rowSelected = treeModel.row_selected()
> switch (rowSelected) {
>   case 0: {
>     ...
>     //if I want to key to somethingelse[rowSelected]
> .......
> currently I think this would have to be done:
> std::string color = row[menuColums.color] 
> if (color == "blue") {...}
>   //if I want to key to somethingelse have to find index of "blue"

The real question is, "what's the value of having an index?"  The answer
is: "so you can lookup something else."  Gtk::TreeModel encourages you
to put the "something else" in the model, where you don't have to look
it up - you can access it directly using the iterator.  If the external
objects that you're looking-up are heavy or don't support value
semantics, just create a column that contains pointers to the originals.
Yes, this requires a change in thinking relative to other APIs, but if
you give it a chance, you will find that it is a very flexible and
elegant way of doing things that makes your code cleaner.

And if you absolutely must have an index to look things up elsewhere,
make the it part of your model.


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