cglib

How do I convert a GList into a GListModel in C?


I'm working on a GTK program written in C. In a specific function, adw_combo_row_set_model (), I need to provide a GListModel. I have a GList. How can I either convert the GList into a GListModel or otherwise provide a view of the GList that is compatible with GListModel?


Solution

  • I need to provide a GListModel. I have a GList. How can I either convert the GList into a GListModel or otherwise provide a view of the GList that is compatible with GListModel?

    Despite "List" appearing in both names, GListModel isn't really related to GList. There is no direct conversion.

    You have two main options:

    1. Choose an existing implementation of GListModel, such as GListStore, instantiate it, and copy the elements of your GList into it.

    2. Write your own implementation of GListModel that wraps a GList and provides a view of it.

    Depending on where the GList is coming from and what else you need to be able to do with it, you might have a secondary option of using a GListModel throughout. Then no special action would be required to get one.