pyqt_reactive.widgets.mixins.selection_preservation_mixin

Selection Preservation Utilities for PyQt6 List Widgets

Simple utility functions for preserving selection when updating QListWidget contents, similar to the ButtonListWidget pattern used in the Textual TUI.

Functions

handle_selection_change_with_prevention(...)

Handle selection changes with automatic deselection prevention.

preserve_selection_during_update(...)

Execute a list update function while preserving selection.

restore_selection_by_id(list_widget, ...)

Restore selection to an item by its identifier.

pyqt_reactive.widgets.mixins.selection_preservation_mixin.preserve_selection_during_update(list_widget: QListWidget, get_identifier_func: Callable[[Any], str], should_preserve_func: Callable[[], bool], update_func: Callable[[], None])[source]

Execute a list update function while preserving selection.

Parameters:
  • list_widget – The QListWidget to update

  • get_identifier_func – Function to extract unique ID from item data

  • should_preserve_func – Function that returns True if selection should be preserved

  • update_func – Function that updates the list widget contents

pyqt_reactive.widgets.mixins.selection_preservation_mixin.restore_selection_by_id(list_widget: QListWidget, item_id: str, get_identifier_func: Callable[[Any], str])[source]

Restore selection to an item by its identifier.

Parameters:
  • list_widget – The QListWidget to update

  • item_id – Identifier of the item to select

  • get_identifier_func – Function to extract unique ID from item data

pyqt_reactive.widgets.mixins.selection_preservation_mixin.handle_selection_change_with_prevention(list_widget: QListWidget, get_selected_func: Callable, get_identifier_func: Callable[[Any], str], should_preserve_func: Callable[[], bool], get_current_id_func: Callable[[], str], on_selected_func: Callable, on_cleared_func: Callable)[source]

Handle selection changes with automatic deselection prevention.

Parameters:
  • list_widget – The QListWidget to manage

  • get_selected_func – Function that returns currently selected items

  • get_identifier_func – Function to extract unique ID from item data

  • should_preserve_func – Function that returns True if deselection should be prevented

  • get_current_id_func – Function that returns the current selection ID

  • on_selected_func – Function to call when items are selected (receives selected items)

  • on_cleared_func – Function to call when selection is cleared (no args)