Skip to content

Gtk.SingleSelection

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

A selection model that allows selecting a single item.

Note that the selection is persistent -- if the selected item is removed and re-added in the same Gio.ListModel.items-changed emission, it stays selected. In particular, this means that changing the sort order of an underlying sort model will preserve the selection.

Constructors

new

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

Creates a new selection to handle model.

Parameters:

  • model — the GListModel to manage

Methods

get_autoselect

def get_autoselect(self) -> bool

Checks if autoselect has been enabled or disabled via SingleSelection.set_autoselect.

get_can_unselect

def get_can_unselect(self) -> bool

If True, SelectionModel.unselect_item is supported and allows unselecting the selected item.

get_model

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

Gets the model that self is wrapping.

get_selected

def get_selected(self) -> int

Gets the position of the selected item.

If no item is selected, INVALID_LIST_POSITION is returned.

get_selected_item

def get_selected_item(self) -> GObject.Object | None

Gets the selected item.

If no item is selected, None is returned.

set_autoselect

def set_autoselect(self, autoselect: bool) -> None

Enables or disables autoselect.

If autoselect is True, self will enforce that an item is always selected. It will select a new item when the currently selected item is deleted and it will disallow unselecting the current item.

Parameters:

  • autoselectTrue to always select an item

set_can_unselect

def set_can_unselect(self, can_unselect: bool) -> None

If True, unselecting the current item via SelectionModel.unselect_item is supported.

Note that setting SingleSelection.autoselect will cause unselecting to not work, so it practically makes no sense to set both at the same time.

Parameters:

  • can_unselectTrue to allow unselecting

set_model

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

Sets the model that self should wrap.

If model is None, self will be empty.

Parameters:

  • model — A GListModel to wrap

set_selected

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

Selects the item at the given position.

If the list does not have an item at position or INVALID_LIST_POSITION is given, the behavior depends on the value of the SingleSelection.autoselect property: If it is set, no change will occur and the old item will stay selected. If it is unset, the selection will be unset and no item will be selected. This also applies if SingleSelection.can-unselect is set to False.

Parameters:

Properties

autoselect

autoselect: bool  # read/write

If the selection will always select an item.

can_unselect

can_unselect: bool  # read/write

If unselecting the selected item is allowed.

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 managed.

n_items

n_items: int  # read-only

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

selected

selected: int  # read/write

Position of the selected item.

selected_item

selected_item: GObject.Object  # read-only

The selected item.