cgtk4

Populating dropdown with strings in GTK4


I have a GHashTable whose keys are strings.

GList *keylist = g_hash_table_get_keys (my_hash);

I want to populate a DropDown with the strings in keylist using a model.

In Gtk > DropDown I see methods such as set_factory, set_list_factory, and set_model, but I'm not able to put the pieces together to populate the dropdown.

In example columnview with strings gtk4 C someone thankfully posted an example for populating a column view from a model with five fields. That example is helpful, but seems complicated for populating a dropdown menu with strings.

Is a simple example or some guidance available for my use case? Thank you.


Solution

  • After many trials and errors, I was able to populate the dropdown with the following few lines:

    guint hash_size;
    gpointer key_array = g_hash_table_get_keys_as_array (hash, &hash_size); /* Get the keys, which are strings, as an array */
    GtkStringList *stringlist = gtk_string_list_new (key_array); /* Create a new GtkStringList model from the array of strings. */
    gtk_drop_down_set_model (GTK_DROP_DOWN(dropdown), G_LIST_MODEL(stringlist)); /* Set the model as the source for the dropdown menu. */
    

    As so often happens, now that I have the code working, I better understand the API in Gtk > DropDown > set_model. A GtkStringList implements GListModel, which we can see from the former's hierarachy. Is there a way to know what classes implement GListModel? That would have reduced my research time.

    Anyway, thanks to all for reading and responding.