From 43d6139452ada433754f68644d5c5293f8d60377 Mon Sep 17 00:00:00 2001 From: Tristan Simas Date: Fri, 27 Nov 2026 11:25:54 -0500 Subject: [PATCH] Use QTextLayout as the single layout engine for WrappedText.heightForWidth and paintEvent --- src/pyqt_reactive/widgets/fitted_text.py | 26 ++++++++++++++++-------- tests/test_fitted_text.py | 21 +++++++++++++++++++ 2 files changed, 38 insertions(+), 9 deletions(-) diff --git a/src/pyqt_reactive/widgets/fitted_text.py b/src/pyqt_reactive/widgets/fitted_text.py index 89d58bf..06b447a 100644 --- a/src/pyqt_reactive/widgets/fitted_text.py +++ b/src/pyqt_reactive/widgets/fitted_text.py @@ -6,6 +6,8 @@ asks for its text's width but needs only its margins; each member decides how text that does not fit is shown, and the whole text stays the tooltip.""" +import math + from PySide6.QtCore import QEvent, QPointF, QRect, QSize, Qt from PySide6.QtGui import QColor, QPainter, QPalette, QTextLayout, QTextOption from PySide6.QtWidgets import QLabel @@ -79,16 +81,12 @@ def minimumSizeHint(self) -> QSize: # noqa: N802 - Qt virtual method name self._min_size_hint = hint return QSize(hint) - def _prepare_text_layouts(self, rect: QRect) -> tuple[QRect, tuple[QTextLayout, ...], QPointF, bool]: - flags, text = self.drawn(rect) - if not text or rect.width() <= 0 or rect.height() <= 0: - cached = (QRect(rect), (), QPointF(), False) - self._text_layouts = cached - return cached + def _layout_text(self, text: str, width: int) -> tuple[tuple[QTextLayout, ...], float]: + """Lay out `text` into `width` pixels with the single QTextLayout engine used for both + painting and wrapped height measurement.""" font = self.font() option = QTextOption(self.alignment()) option.setWrapMode(self.WRAP_MODE) - width = rect.width() layouts: list[QTextLayout] = [] y = 0.0 for paragraph in text.split("\n"): @@ -105,6 +103,15 @@ def _prepare_text_layouts(self, rect: QRect) -> tuple[QRect, tuple[QTextLayout, y += line.height() layout.endLayout() layouts.append(layout) + return tuple(layouts), y + + def _prepare_text_layouts(self, rect: QRect) -> tuple[QRect, tuple[QTextLayout, ...], QPointF, bool]: + flags, text = self.drawn(rect) + if not text or rect.width() <= 0 or rect.height() <= 0: + cached = (QRect(rect), (), QPointF(), False) + self._text_layouts = cached + return cached + layouts, y = self._layout_text(text, rect.width()) if flags & self.ALIGN_VCENTER: top = rect.top() + (rect.height() - y) / 2.0 elif flags & self.ALIGN_BOTTOM: @@ -112,7 +119,7 @@ def _prepare_text_layouts(self, rect: QRect) -> tuple[QRect, tuple[QTextLayout, else: top = float(rect.top()) needs_clip = y > rect.height() or rect != self.rect() - cached = (QRect(rect), tuple(layouts), QPointF(float(rect.left()), top), needs_clip) + cached = (QRect(rect), layouts, QPointF(float(rect.left()), top), needs_clip) self._text_layouts = cached return cached @@ -180,7 +187,8 @@ def _clear_metrics_cache(self) -> None: def text_rect(self, width: int) -> QRect: margins = self.contentsMargins() inner = max(width - margins.left() - margins.right(), 1) - return self.fontMetrics().boundingRect(QRect(0, 0, inner, 1 << 20), self.WRAP_FLAGS, self.text()) + _, y = self._layout_text(self.text(), inner) + return QRect(0, 0, inner, math.ceil(y)) def heightForWidth(self, width: int) -> int: # noqa: N802 - Qt virtual method name cached = self._height_for_width diff --git a/tests/test_fitted_text.py b/tests/test_fitted_text.py index 136984a..3475d7d 100644 --- a/tests/test_fitted_text.py +++ b/tests/test_fitted_text.py @@ -126,3 +126,24 @@ def counting_text_rect(width: int): assert text_rect_calls == 3 assert shorter < bigger assert widget.sizeHint().height() <= bigger + + +def test_wrapped_text_height_matches_painted_qtextlayout_without_clipping(qtbot): + """heightForWidth and paintEvent share QTextLayout so narrow punctuation-heavy text + never wraps into more lines during paint than heightForWidth reserved.""" + text = ( + "Objective: Ship Android/Qt Toad mobile UX fixes (IME adjustResize/safe-area, " + "tab close, sidebar bands), profile & fix Pixel 8a scroll FPS with FrameProbe, " + "and keep Qt/Textual shared-frontend hoists & NRA Toad bench moving" + ) + widget = WrappedText(text) + qtbot.addWidget(widget) + for width in (140, 180, 207, 260, 360): + height = widget.heightForWidth(width) + widget.resize(width, height) + widget.grab() + cached = widget._text_layouts + assert cached is not None + _rect, _layouts, origin, needs_clip = cached + assert not needs_clip, f"WrappedText clipped at width={width} height={height}" + assert origin.y() >= 0.0