Skip to content

Gtk.StringList

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

A list model that wraps an array of strings.

The objects in the model are of type StringObject and have a "string" property that can be used inside expressions.

GtkStringList is well-suited for any place where you would typically use a char*[], but need a list model.

GtkStringList as GtkBuildable

The GtkStringList implementation of the GtkBuildable interface supports adding items directly using the <items> element and specifying <item> elements for each item. Each <item> element supports the regular translation attributes “translatable”, “context” and “comments”.

Here is a UI definition fragment specifying a GtkStringList

<object class="GtkStringList">
  <items>
    <item translatable="yes">Factory</item>
    <item translatable="yes">Home</item>
    <item translatable="yes">Subway</item>
  </items>
</object>

Constructors

new

@classmethod
def new(cls, strings: list[str] | None = ...) -> StringList

Creates a new GtkStringList with the given strings.

Parameters:

  • strings — The strings to put in the model

Methods

append

def append(self, string: str) -> None

Appends string to self.

The string will be copied. See StringList.take for a way to avoid that.

Parameters:

  • string — the string to insert

find

def find(self, string: str) -> int

Gets the position of the string in self.

If self does not contain string item, G_MAXUINT is returned.

Parameters:

  • string — the string to find

get_string

def get_string(self, position: int) -> str | None

Gets the string that is at position in self.

If self does not contain position items, None is returned.

This function returns the const char *. To get the object wrapping it, use g_list_model_get_item().

Parameters:

  • position — the position to get the string for

remove

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

Removes the string at position from self.

position must be smaller than the current length of the list.

Parameters:

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

splice

def splice(self, position: int, n_removals: int, additions: list[str] | None = ...) -> None

Changes self by removing n_removals strings and adding additions to it.

This function is more efficient than StringList.append and StringList.remove, because it only emits the ::items-changed signal once for the change.

This function copies the strings 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 strings to remove
  • additions — The strings to add

take

def take(self, string: str) -> None

Adds string to self at the end, and takes ownership of it.

This variant of StringList.append is convenient for formatting strings:

gtk_string_list_take (self, g_strdup_print ("%d dollars", lots));

Parameters:

  • string — the string to insert

Properties

item_type

item_type: type | GObject.Type  # read-only

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

n_items

n_items: int  # read-only

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

strings

strings: list[str]  # read/write

The strings in the model.