From 287854d014fa3818fed2c9c23a106595e0170974 Mon Sep 17 00:00:00 2001 From: RumovZ Date: Tue, 28 Sep 2021 10:19:33 +0200 Subject: [PATCH] Prefer `selectionModel().reset()` over `.clear()` The latter triggers `selectionChanged()` unreliably, probably due to the aggregation of chronologically close events, causing problems in tracking `_len_selection`. `reset()` never emits signals. --- qt/aqt/browser/table/table.py | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/qt/aqt/browser/table/table.py b/qt/aqt/browser/table/table.py index 8e7585c40..436429049 100644 --- a/qt/aqt/browser/table/table.py +++ b/qt/aqt/browser/table/table.py @@ -132,10 +132,12 @@ class Table: def select_single_card(self, card_id: CardId) -> None: """Try to set the selection to the item corresponding to the given card.""" - self.clear_selection() + self._reset_selection() if (row := self._model.get_card_row(card_id)) is not None: self._view.selectRow(row) self._scroll_to_row(row, scroll_even_if_visible=True) + else: + self.browser.on_row_changed() # Reset @@ -223,8 +225,8 @@ class Table: if nid is not None and nid not in nids: self._move_current_to_row(row) return - self.clear_selection() - self.clear_current() + self._reset_selection() + self.browser.on_row_changed() def clear_current(self) -> None: self._view.selectionModel().setCurrentIndex( @@ -248,6 +250,11 @@ class Table: ) self._view.selectionModel().setCurrentIndex(index, QItemSelectionModel.NoUpdate) + def _reset_selection(self) -> None: + """Remove selection and focus without emitting signals.""" + self._view.selectionModel().reset() + self._len_selection = 0 + def _select_rows(self, rows: List[int]) -> None: selection = QItemSelection() for row in rows: @@ -470,7 +477,7 @@ class Table: """Restore the saved selection and current element as far as possible and scroll to the new current element. Clear the saved selection. """ - self.clear_selection() + self._reset_selection() if not self._model.is_empty(): rows, current = new_selected_and_current() rows = self._qualify_selected_rows(rows, current)