Search code examples
c++comboboxgtkassociative-arraygtkmm

How can I associate data - a string, integer or enum member - with a Gtk::ComboBoxText item?


I am using gktmm 3.0 on an Ubuntu 12.04 box with the default GCC toolchain.

In the C# world, the ComboBox class has a ComboxBox.item[n].value property, which allows you to associate each item in the comboBox with data.

I am looking for something similar in the Gtk::ComboBoxTextclass. How can I associate data - a string, integer or enum member for example - with a particular Gtk::ComboBoxTextitem?

I know that many frameworks provide a genericdata pointer on widgets for such use, as this is quite a common need.

Is there something in Gtk::ComboBoxText class or one of its parent classes that might allow me accomplish this, or do I need to set up such an association myself, using a map or other associative collection?


Solution

  • The Gtk::ComboBoxText append, insert() and prepend() methods allows you to specify an ID string as well as the human-visible text. For instance: https://developer.gnome.org/gtkmm/stable/classGtk_1_1ComboBoxText.html#a19e80f4e451e23d2c00d3fb11023f9f2

    But it would be clearer and more type-safe to use Gtk::ComboBox and define an actual underlying model that contains the associated data. This example uses an int, but you could use other types or use more columns: https://developer.gnome.org/gtkmm-tutorial/stable/combobox-example-full.html.en