:right-sidebar: True SingleSelection =================================================================== .. currentmodule:: gi.repository.Gtk .. class:: SingleSelection(**properties: ~typing.Any) :no-contents-entry: Superclasses: :class:`~gi.repository.GObject.Object` Implemented Interfaces: :class:`~gi.repository.Gio.ListModel`, :class:`~gi.repository.Gtk.SectionModel`, :class:`~gi.repository.Gtk.SelectionModel` ``GtkSingleSelection`` is a ``GtkSelectionModel`` that allows selecting a single item. Note that the selection is *persistent* -- if the selected item is removed and re-added in the same :obj:`~gi.repository.Gio.ListModel.signals.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 ------------ .. rst-class:: interim-class .. class:: SingleSelection :no-index: .. classmethod:: new(model: ~gi.repository.Gio.ListModel | None = None) -> ~gi.repository.Gtk.SingleSelection Creates a new selection to handle ``model``. :param model: the ``GListModel`` to manage Methods ------- .. rst-class:: interim-class .. class:: SingleSelection :no-index: .. method:: get_autoselect() -> bool Checks if autoselect has been enabled or disabled via :func:`~gi.repository.Gtk.SingleSelection.set_autoselect`. .. method:: get_can_unselect() -> bool If :const:`True`, :func:`~gi.repository.Gtk.SelectionModel.unselect_item` is supported and allows unselecting the selected item. .. method:: get_model() -> ~gi.repository.Gio.ListModel | None Gets the model that ``self`` is wrapping. .. method:: get_selected() -> int Gets the position of the selected item. If no item is selected, :const:`~gi.repository.Gtk.INVALID_LIST_POSITION` is returned. .. method:: get_selected_item() -> ~gi.repository.GObject.Object | None Gets the selected item. If no item is selected, :const:`None` is returned. .. method:: set_autoselect(autoselect: bool) -> None Enables or disables autoselect. If ``autoselect`` is :const:`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. :param autoselect: :const:`True` to always select an item .. method:: set_can_unselect(can_unselect: bool) -> None If :const:`True`, unselecting the current item via :func:`~gi.repository.Gtk.SelectionModel.unselect_item` is supported. Note that setting :obj:`~gi.repository.Gtk.SingleSelection.props.autoselect` will cause unselecting to not work, so it practically makes no sense to set both at the same time the same time. :param can_unselect: :const:`True` to allow unselecting .. method:: set_model(model: ~gi.repository.Gio.ListModel | None = None) -> None Sets the model that ``self`` should wrap. If ``model`` is :const:`None`, ``self`` will be empty. :param model: A ``GListModel`` to wrap .. method:: set_selected(position: int) -> None Selects the item at the given position. If the list does not have an item at ``position`` or :const:`~gi.repository.Gtk.INVALID_LIST_POSITION` is given, the behavior depends on the value of the :obj:`~gi.repository.Gtk.SingleSelection.props.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. :param position: the item to select or :const:`~gi.repository.Gtk.INVALID_LIST_POSITION` Properties ---------- .. rst-class:: interim-class .. class:: SingleSelection :no-index: .. attribute:: props.autoselect :type: bool If the selection will always select an item. .. attribute:: props.can_unselect :type: bool If unselecting the selected item is allowed. .. attribute:: props.item_type :type: type The type of items. See :obj:`~gi.repository.Gio.ListModel.get_item_type`. .. versionadded:: 4.8 .. attribute:: props.model :type: ~gi.repository.Gio.ListModel The model being managed. .. attribute:: props.n_items :type: int The number of items. See :obj:`~gi.repository.Gio.ListModel.get_n_items`. .. versionadded:: 4.8 .. attribute:: props.selected :type: int Position of the selected item. .. attribute:: props.selected_item :type: ~gi.repository.GObject.Object The selected item.