Re: Gtk2::ComboBox - setting value by string?

On Aug 8, 2006, at 10:17 PM, Daniel Kasak wrote:

John M. Burian wrote:
It doesn't fix the 'bug', such as it is, but a technique I use is to, as I'm adding values to the ListStore, create a hash that maps the strings back to the indicies of the ListStore, then add a reference to that hash
to the ComboBox object.

$list = Gtk2::ListStore->new ("Glib::String");
map {
  $list->set ($iter = $list->append, 0, $_);
  $$href{$_} = $list->get_string_from_iter ($iter);
} (qw/Option1 Option2 Option3/);
$combobox = Gtk2::ComboBox->new_with_model ($list);
$combobox->{hash} = $href;

Then later, to set the value of the combobox using the string:

$combobox->set_active ($combobox->{hash}{'Option1'});

This relies on both perl's implicit conversion from strings to integers and the fact that list paths have only one index, so the path string converts cleanly to an integer.

Are you allowed to do this? I've read in a number of places that it's
dangerous to keep iters around for any length of time ... ie that they
are only valid immediately.

He's calling Gtk2::TreeModel::get_string_from_iter(), which returns "a string representation of an iter":

This is actually equivalent to $model->get_path ($iter)->to_string (). It's safe to save treepath objects, because they are just lists of integer indices, but it is important to remember that the indices are invalid when the model is altered.

That's it! It's one thing for a ghost to scare my children, but it's another to play my theremin!
  - Homer Simpson

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