Skip to content

Gio.ListStore

class — extends GObject.Object, ListModel

GListStore is a simple implementation of ListModel that stores all items in memory.

It provides insertions, deletions, and lookups in logarithmic time with a fast path for the common case of iterating the list linearly.

Constructors

new

@classmethod
def new(cls, item_type: type | GObject.Type) -> ListStore

Creates a new ListStore with items of type item_type. item_type must be a subclass of GObject.Object.

Parameters:

  • item_type — the GType of items in the list

Methods

append

def append(self, item: GObject.Object) -> None

Appends item to store. item must be of type ListStore:item-type.

This function takes a ref on item.

Use ListStore.splice to append multiple items at the same time efficiently.

Parameters:

  • item — the new item

find

def find(self, item: GObject.Object) -> tuple[bool, int]

Looks up the given item in the list store by looping over the items until the first occurrence of item. If item was not found, then position will not be set, and this method will return False.

If you need to compare the two items with a custom comparison function, use ListStore.find_with_equal_func with a custom GEqualFunc instead.

Parameters:

  • item — an item

find_with_equal_func

def find_with_equal_func(self, item: GObject.Object | None, equal_func: GLib.EqualFunc) -> tuple[bool, int]

Looks up the given item in the list store by looping over the items and comparing them with equal_func until the first occurrence of item which matches. If item was not found, then position will not be set, and this method will return False.

item is always passed as second parameter to equal_func.

Since GLib 2.76 it is possible to pass NULL for item.

Parameters:

  • item — an item
  • equal_func — A custom equality check function

find_with_equal_func_full

def find_with_equal_func_full(self, item: GObject.Object | None, equal_func: GLib.EqualFuncFull) -> tuple[bool, int]

Like ListStore.find_with_equal_func but with an additional user_data that is passed to equal_func.

item is always passed as second parameter to equal_func.

Since GLib 2.76 it is possible to pass NULL for item.

Parameters:

  • item — an item
  • equal_func — A custom equality check function

insert

def insert(self, position: int, item: GObject.Object) -> None

Inserts item into store at position. item must be of type ListStore:item-type or derived from it. position must be smaller than the length of the list, or equal to it to append.

This function takes a ref on item.

Use ListStore.splice to insert multiple items at the same time efficiently.

Parameters:

  • position — the position at which to insert the new item
  • item — the new item

insert_sorted

def insert_sorted(self, item: GObject.Object, compare_func: GLib.CompareDataFunc) -> int

Inserts item into store at a position to be determined by the compare_func.

The list must already be sorted before calling this function or the result is undefined. Usually you would approach this by only ever inserting items by way of this function.

This function takes a ref on item.

Parameters:

  • item — the new item
  • compare_func — pairwise comparison function for sorting

remove

def remove(self, position: int) -> None

Removes the item from store that is at position. position must be smaller than the current length of the list.

Use ListStore.splice to remove multiple items at the same time efficiently.

Parameters:

  • position — the position of the item that is to be removed

remove_all

def remove_all(self) -> None

Removes all items from store.

sort

def sort(self, compare_func: GLib.CompareDataFunc) -> None

Sort the items in store according to compare_func.

Parameters:

  • compare_func — pairwise comparison function for sorting

splice

def splice(self, position: int, n_removals: int, additions: list[GObject.Object]) -> None

Changes store by removing n_removals items and adding n_additions items to it. additions must contain n_additions items of type ListStore:item-type. None is not permitted.

This function is more efficient than ListStore.insert and ListStore.remove, because it only emits ListModel::items-changed once for the change.

This function takes a ref on each item in additions.

The parameters position and n_removals must be correct (ie: position + n_removals must be less than or equal to the length of the list at the time this function is called).

Parameters:

  • position — the position at which to make the change
  • n_removals — the number of items to remove
  • additions — the items to add

Properties

item_type

item_type: type | GObject.Type  # read/write

The type of items contained in this list store. Items must be subclasses of GObject.Object.

n_items

n_items: int  # read-only

The number of items contained in this list store.