From 890b709be75c57de7707b63eb88b008ff3813e80 Mon Sep 17 00:00:00 2001 From: Tristan Simas Date: Sat, 21 Nov 2026 19:58:49 -0500 Subject: [PATCH] virtual rows: a row's hint is answered from its kept key A layout asks every row's size. A row whose size is kept answered at once, but a subclass that reports a wider hint than the layout width (a view that scrolls sideways, widening by `row_extent`) had to override `sizeHint`, and its override built the row's key again for every row on every layout, kept size or not: 129,813 key builds while a reader scrolled back through 216 rows. `hinted(key, size)` is what the view is told a row takes; the base answers the size, and every path of `sizeHint` returns through it with the key it already holds (the kept row's own key on the fast path, `kept_row`). A subclass widens there and builds nothing. Co-Authored-By: Claude Fable 5.2 Claude-Session: https://claude.ai/code/session_016gZLdx92gKjvnNKianjAXG --- src/pyqt_reactive/widgets/virtual_rows.py | 29 +++++++++++----- tests/test_virtual_rows.py | 42 ++++++++++++++++++++++- 2 files changed, 62 insertions(+), 9 deletions(-) diff --git a/src/pyqt_reactive/widgets/virtual_rows.py b/src/pyqt_reactive/widgets/virtual_rows.py index 10cdae5..36e246d 100644 --- a/src/pyqt_reactive/widgets/virtual_rows.py +++ b/src/pyqt_reactive/widgets/virtual_rows.py @@ -501,14 +501,27 @@ def rows_data_changed(self, first: int, last: int) -> None: for row in range(first, min(last + 1, len(sizes))): sizes[row] = None - def kept_size(self, row: int, *, measured: bool = False) -> QSize | None: - """The row's kept size while a layout runs (only a measured one when `measured`).""" + def kept_row(self, row: int, *, measured: bool = False) -> RowSize | None: + """The row's kept size and the key it was measured under, while a layout runs (only a + measured one when `measured`).""" if self._layout_for is None or row >= len(self._row_sizes): return None kept = self._row_sizes[row] if kept is None or kept.size is None or (measured and kept.estimated): return None - return kept.size + return kept + + def kept_size(self, row: int, *, measured: bool = False) -> QSize | None: + kept = self.kept_row(row, measured=measured) + return None if kept is None else kept.size + + def hinted(self, key: Hashable, size: QSize) -> QSize: + """What the view is told a row takes, from its size at the layout width: that size. + A view that scrolls sideways answers the row's extent as the width (`row_extent`). + A layout asks this of every row, so it is answered from the kept key and size with + nothing built: a subclass that overrode `sizeHint` to widen its rows built each + row's key again on every layout, kept size or not.""" + return size def keep_row_size( self, row: int, key: Hashable, size: QSize, font: QFont, *, estimated: bool = False @@ -575,8 +588,8 @@ def painted_document(self, index: QModelIndex, option: QStyleOptionViewItem) -> return self.final_document(index, option.font, option.rect.width()) def sizeHint(self, option, index) -> QSize: # type: ignore[override] - if (kept := self.kept_size(index.row())) is not None: - return kept + if (kept := self.kept_row(index.row())) is not None: + return self.hinted(kept.key, kept.size) opt = QStyleOptionViewItem(option) self.initStyleOption(opt, index) width = self.available_width(opt) @@ -584,7 +597,7 @@ def sizeHint(self, option, index) -> QSize: # type: ignore[override] size = self._size_hints.get((key, width)) if size is not None: self.keep_row_size(index.row(), key, size, opt.font) - return size + return self.hinted(key, size) if self.view.estimating(): # Away from the reader: an estimate now, measured between frames. self._unmeasured[QPersistentModelIndex(index)] = None @@ -592,8 +605,8 @@ def sizeHint(self, option, index) -> QSize: # type: ignore[override] self._measure_timer.start() size = self.estimated_size(index, opt.font, width) self.keep_row_size(index.row(), key, size, opt.font, estimated=True) - return size - return self.measured_size(index, opt.font, width) + return self.hinted(key, size) + return self.hinted(key, self.measured_size(index, opt.font, width)) def measured_size(self, index: QModelIndex, font: QFont, width: int) -> QSize: """The row's height laid out at `width`, kept; a pending row's estimate, not kept, so diff --git a/tests/test_virtual_rows.py b/tests/test_virtual_rows.py index 7c9075f..b10e6df 100644 --- a/tests/test_virtual_rows.py +++ b/tests/test_virtual_rows.py @@ -6,7 +6,7 @@ from dataclasses import dataclass from functools import partial -from PySide6.QtCore import QStringListModel, Qt +from PySide6.QtCore import QSize, QStringListModel, Qt from PySide6.QtGui import QFont, QGuiApplication, QTextDocument from pyqt_reactive.widgets.virtual_rows import PendingRow, RowDocumentDelegate, RowFormatting, VirtualRowView @@ -180,6 +180,46 @@ def test_a_rows_extent_is_recorded_when_it_is_laid_out(qtbot) -> None: delegate.cleanup() +class WideRows(BoldRows): + """Rows of a view that scrolls sideways: a row's hint is as wide as its content.""" + + def preparation(self, index, font, width): + return partial(UnwrappedRow, self.row_text(index)) + + def hinted(self, key, size): + return QSize(self.row_extent(key, size.width()), size.height()) + + +def test_a_layout_answers_each_rows_hint_from_its_kept_key(qtbot) -> None: + """A layout asks every row's size. A row whose size is kept answers from the kept key and + size, widened by the subclass's own `hinted`, without building the row's key again.""" + model = QStringListModel(["x" * 200, "short"]) + view = VirtualRowView(WideRows()) + qtbot.addWidget(view) + view.setModel(model) + view.resize(300, 400) + delegate = view.rows + try: + view.show() + qtbot.waitUntil(lambda: all( + delegate.is_measured(model.index(row, 0), view.font(), view.viewport_width()) + for row in range(2)), timeout=3000) + view.doItemsLayout() + width = view.viewport_width() + assert view.sizeHintForIndex(model.index(0, 0)).width() > width + assert view.sizeHintForIndex(model.index(1, 0)).width() == width + built = [] + preparation_key = delegate.preparation_key + def counted(*arguments): + built.append(arguments) + return preparation_key(*arguments) + delegate.preparation_key = counted + view.doItemsLayout() + assert built == [] + finally: + delegate.cleanup() + + def test_a_page_of_rows_arrives_as_one_batch(qtbot) -> None: """Rows queued together are worked out by one drain of the pool's queue and reach the GUI thread together, so the view lays them out once rather than once per row."""