Skip to content

Gtk.MapListModel

class — extends GObject.Object, Gio.ListModel, SectionModel

A list model that maps the items in another model to different items.

GtkMapListModel uses a MapListModelMapFunc.

Example: Create a list of GtkEventControllers

static gpointer
map_to_controllers (gpointer widget,
                    gpointer data)
{
  gpointer result = gtk_widget_observe_controllers (widget);
  g_object_unref (widget);
  return result;
}

widgets = gtk_widget_observe_children (widget);

controllers = gtk_map_list_model_new (widgets,
                                      map_to_controllers,
                                      NULL, NULL);

model = gtk_flatten_list_model_new (GTK_TYPE_EVENT_CONTROLLER,
                                    controllers);

GtkMapListModel will attempt to discard the mapped objects as soon as they are no longer needed and recreate them if necessary.

GtkMapListModel passes through sections from the underlying model.

Constructors

new

@classmethod
def new(cls, model: Gio.ListModel | None = ..., map_func: MapListModelMapFunc | None = ...) -> MapListModel

Creates a new GtkMapListModel for the given arguments.

Parameters:

  • model — The model to map
  • map_func — map function

Methods

get_model

def get_model(self) -> Gio.ListModel | None

Gets the model that is currently being mapped or None if none.

has_map

def has_map(self) -> bool

Checks if a map function is currently set on self.

set_map_func

def set_map_func(self, map_func: MapListModelMapFunc | None = ...) -> None

Sets the function used to map items.

The function will be called whenever an item needs to be mapped and must return the item to use for the given input item.

Note that GtkMapListModel may call this function multiple times on the same item, because it may delete items it doesn't need anymore.

GTK makes no effort to ensure that map_func conforms to the item type of self. It assumes that the caller knows what they are doing and the map function returns items of the appropriate type.

Parameters:

  • map_func — map function

set_model

def set_model(self, model: Gio.ListModel | None = ...) -> None

Sets the model to be mapped.

GTK makes no effort to ensure that model conforms to the item type expected by the map function. It assumes that the caller knows what they are doing and have set up an appropriate map function.

Parameters:

  • model — The model to be mapped

Properties

has_map

has_map: bool  # read-only

If a map is set for this model

item_type

item_type: type | GObject.Type  # read-only

The type of items. See Gio.ListModel.get_item_type.

model

model: Gio.ListModel  # read/write

The model being mapped.

n_items

n_items: int  # read-only

The number of items. See Gio.ListModel.get_n_items.