GListModel: GIO Reference Manual

## GListModel GListModel — An interface describing a dynamic list of objects |

## Functions

GType | g_list_model_get_item_type ()
guint | g_list_model_get_n_items ()
gpointer | g_list_model_get_item ()
GObject * | g_list_model_get_object ()
void | g_list_model_items_changed ()

## Signals

void | items-changed | Run Last

## Types and Values

| GListModel
struct | GListModelInterface

## Object Hierarchy

        GInterface
        ╰── GListModel

## Prerequisites

GListModel requires GObject.

## Known Implementations

GListModel is implemented by GListStore.

## Includes

    #include <gio/gio.h>

## Description

GListModel is an interface that represents a mutable list of GObjects. Its main intention is as a model for various widgets in user interfaces, such as list views, but it can also be used as a convenient method of returning lists of data, with support for updates.

Each object in the list may also report changes in itself via some mechanism (normally the “notify” signal). Taken together with the “items-changed” signal, this provides for a list that can change its membership, and in which the members can change their individual properties.

A good example would be the list of visible wireless network access points, where each access point can report dynamic properties such as signal strength.

It is important to note that the GListModel itself does not report changes to the individual items. It only reports changes to the list membership. If you want to observe changes to the objects themselves then you need to connect signals to the objects that you are interested in.

All items in a GListModel are of (or derived from) the same type. `g_list_model_get_item_type()` returns that type. The type may be an interface, in which case all objects in the list must implement it.

The semantics are close to that of an array: `g_list_model_get_length()` returns the number of items in the list and `g_list_model_get_item()` returns an item at a (0-based) position. In order to allow implementations to calculate the list length lazily, you can also iterate over items: starting from 0, repeatedly call `g_list_model_get_item()` until it returns `NULL`.

An implementation may create objects lazily, but must take care to return the same object for a given position until all references to it are gone.

On the other side, a consumer is expected only to hold references on objects that are currently "user visible", in order to faciliate the maximum level of laziness in the implementation of the list and to reduce the required number of signal connections at a given time.

This interface is intended only to be used from a single thread. The thread in which it is appropriate to use it depends on the particular implementation, but typically it will be from the thread that owns the thread-default main context in effect at the time that the model was created.

## Functions

### g_list_model_get_item_type ()

    GType
    g_list_model_get_item_type (GListModel *list);

Gets the type of the items in `list` . All items returned from `g_list_model_get_type()` are of that type or a subtype, or are an implementation of that interface.

The item type of a GListModel can not change during the life of the model.

#### Parameters

list | a GListModel |

#### Returns

the GType of the items contained in `list` .

Since 2.44

### g_list_model_get_n_items ()

    guint
    g_list_model_get_n_items (GListModel *list);

Gets the number of items in `list` .

Depending on the model implementation, calling this function may be less efficient than iterating the list with increasing values for `position` until `g_list_model_get_item()` returns `NULL`.

#### Parameters

list | a GListModel |

#### Returns

the number of items in `list` .

Since 2.44

### g_list_model_get_item ()

    gpointer
    g_list_model_get_item (GListModel *list,
                           guint position);

Get the item at `position` . If `position` is greater than the number of items in `list` , `NULL` is returned.

`NULL` is never returned for an index that is smaller than the length of the list. See `g_list_model_get_n_items()`.

#### Parameters

list | a GListModel |
position | the position of the item to fetch |

#### Returns

the item at `position` .

[transfer full][nullable][type GObject]

Since 2.44

### g_list_model_get_object ()

    GObject *
    g_list_model_get_object (GListModel *list,
                             guint position);

Get the item at `position` . If `position` is greater than the number of items in `list` , `NULL` is returned.

`NULL` is never returned for an index that is smaller than the length of the list. See `g_list_model_get_n_items()`.

#### Parameters

list | a GListModel |
position | the position of the item to fetch |

#### Returns

the object at `position` .

[transfer full][nullable]

Since 2.44

### g_list_model_items_changed ()

    void
    g_list_model_items_changed (GListModel *list,
                                guint position,
                                guint removed,
                                guint added);

Emits the “items-changed” signal on `list` .

This function should only be called by classes implementing GListModel. It has to be called after the internal representation of `list` has been updated, because handlers connected to this signal might query the new state of the list.

Implementations must only make changes to the model (as visible to its consumer) in places that will not cause problems for that consumer. For models that are driven directly by a write API (such as GListStore), changes can be reported in response to uses of that API. For models that represent remote data, changes should only be made from a fresh mainloop dispatch. It is particularly not permitted to make changes in response to a call to the GListModel consumer API.

Stated another way: in general, it is assumed that code making a series of accesses to the model via the API, without returning to the mainloop, and without calling other code, will continue to view the same contents of the model.

#### Parameters

list | a GListModel |
position | the position at which `list` changed |
removed | the number of items removed |
added | the number of items added |

Since 2.44

## Types and Values

### GListModel

    typedef struct _GListModel GListModel;

GListModel is an opaque data structure and can only be accessed using the following functions.

### struct GListModelInterface

    struct GListModelInterface {
      GTypeInterface g_iface;

      GType     (* get_item_type)   (GListModel *list);

      guint     (* get_n_items)     (GListModel *list);

      gpointer  (* get_item)        (GListModel *list,
                                     guint       position);
    };

The virtual function table for GListModel.

#### Members

GTypeInterface `g_iface`; | parent GTypeInterface |
`get_item_type` () | the virtual function pointer for `g_list_model_get_item_type()` |
`get_n_items` () | the virtual function pointer for `g_list_model_get_n_items()` |
`get_item` () | the virtual function pointer for `g_list_model_get_item()` |

Since 2.44

## Signal Details

### The `“items-changed”` signal

    void
    user_function (GListModel *list,
                   guint       position,
                   guint       removed,
                   guint       added,
                   gpointer    user_data)

This signal is emitted whenever items were added or removed to `list` . At `position` , `removed` items were removed and `added` items were added in their place.

#### Parameters

list | the GListModel that changed |
position | the position at which `list` changed |
removed | the number of items removed |
added | the number of items added |
user_data | user data set when the signal handler was connected. |

Flags: Run Last

Since 2.44

## See Also

GListStore